Wanderer
Posts: 168
Joined: 08 May 2017, 18:36

Re: A data binding question

14 Feb 2018, 17:22

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++
// !-- 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); // !-- 
	}
};
// /* MAINGRID.cpp*/ //
MainGrid::MainGrid()
{
	this->SetDataContext(MyDelegate::m_LangModel); // m_LangModel is LanguageModel class
	// ...
}
<!-- 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"
    >
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:
// "{Binding ElementName=MainGrid, Path=DataContext.Language[nameBunding]}"
<GroupBox Header="{Binding ElementName=MainGrid, Path=DataContext.Language[h_options]}" >
 
User avatar
sfernandez
Site Admin
Posts: 3008
Joined: 22 Dec 2011, 19:20

Re: A data binding question

20 Feb 2018, 19:25

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?
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.
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:
<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>
And if items are added/removed dynamically, they are also connected/disconnected from the UI elements.
 
UE4
Topic Author
Posts: 62
Joined: 29 Dec 2017, 06:32

Re: A data binding question

01 Mar 2018, 14:11

How to find the TouchButton in the ItemsControl?
 
User avatar
sfernandez
Site Admin
Posts: 3008
Joined: 22 Dec 2011, 19:20

Re: A data binding question

05 Mar 2018, 21:22

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:
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