Re: A data binding question
If you asked me, I made maybe mistake and my examples working only for me, because I use it for language and My DataContext is set in codebehind. I'll give you how I done language and you try get idea for your needs, I think it is similar scenario where other elements have same data context, but in different views, windows etc..
in C++
Inside MainGrid I have another grid where is whole GUI, and this MainGrid is use for display other views.
And all bindings from Language have this code:
in C++
Code: Select all
// !-- important things
// /* LANGUAGE.h*/ //
NS_DECLARE_SYMBOL(Language); // !--
class LanguageModel : public Noesis::BaseComponent, public Noesis::INotifyPropertyChanged
{
public:
/// ...
NS_IMPLEMENT_INTERFACE_FIXUP;
private:
// ...
Noesis::ResourceDictionary * m_RD;
///
Noesis::PropertyChangedEventHandler changed;
void Changed(NsSymbol prop);
NS_IMPLEMENT_INLINE_REFLECTION(LanguageModel, Noesis::BaseComponent)
{
NsMeta<Noesis::TypeId>("languageModel");
NsImpl<Noesis::INotifyPropertyChanged>();
NsProp("Language" , &LanguageModel::m_RD); // !--
}
};
Code: Select all
// /* MAINGRID.cpp*/ //
MainGrid::MainGrid()
{
this->SetDataContext(MyDelegate::m_LangModel); // m_LangModel is LanguageModel class
// ...
}
Code: Select all
<!-- XAML MainGrid -->
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Name="MainGrid"
x:Class="MainGrid"
>
And all bindings from Language have this code:
Code: Select all
// "{Binding ElementName=MainGrid, Path=DataContext.Language[nameBunding]}"
<GroupBox Header="{Binding ElementName=MainGrid, Path=DataContext.Language[h_options]}" >
-
sfernandez
Site Admin
- Posts: 3008
- Joined:
Re: A data binding question
Items of the collection of any ItemsControl control are automatically set as DataContext of the item container and item template, you don't need to set it in code.so, I have to call SetView everytime add/remove a array element in DataContext?
the child elements is defined in a template, and initialized at runtime, I don't its name, the FindName may be helpless?
For example, if you have a Person class with Name and Address properties, and a collection of Persons in the page view model, then you can have something like this:
Code: Select all
<ItemsControl ItemsSource="{Binding Persons}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name:" Width="60"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Text="Address:" Width="60"/>
<TextBlock Text="{Binding Address}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Re: A data binding question
How to find the TouchButton in the ItemsControl?
-
sfernandez
Site Admin
- Posts: 3008
- Joined:
Re: A data binding question
You shouldn't need to look for any element in the template when using MVVM, you just bind ViewModel properties/commands to the UI elements to update everything.
But in case you need to find an element in the item template then you have to search inside the item container:
But in case you need to find an element in the item template then you have to search inside the item container:
Code: Select all
TouchButton* FindItemTouchButton(ItemsControl* itemsControl, BaseComponent* item)
{
ItemContainerGenerator* generator = itemsControl->GetItemContainerGenerator();
DependencyObject* container = generator->ContainerFromItem(item);
return FindTouchButton(NsDynamicCast<Visual*>(container));
}
TouchButton* FindTouchButton(Visual* visual)
{
if (visual != 0)
{
TouchButton* touchButton = NsDynamicCast<TouchButton*>(visual);
if (touchButton != 0) return touchButton;
int numChildren = VisualTreeHelper::GetChildrenCount(visual);
for (int i = 0; i < numChildren; ++i)
{
touchButton = FindTouchButton(VisualTreeHelper::GetChild(visual, i));
if (touchButton != 0) return touchButton;
}
}
}
Who is online
Users browsing this forum: Bing [Bot] and 3 guests