UserControl菜单上的多级数据绑定依赖于彼此 [英] Multi-level databinding on UserControl menu dependant on each other

查看:113
本文介绍了UserControl菜单上的多级数据绑定依赖于彼此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个属性 Tours




  • 旅游是一个 ObservableCollection 旅游


    • 每个Tour都有一个 ObservableCollection 派对 Partie


      • 每个 Partie 有一个 ObservableCollection Equipes Equipe





我有3个菜单:


  1. 第一个是与财产绑定旅游

  2. 第二个必须与 SelectedItem 属性的第一个菜单(所以它有一个 ObservableCollection Partie

  3. 必须与第二个菜单的 SelectedItem 属性绑定。 (所以它有一个 ObservableCollection Equipes

现在,这是工作代码:

 < StackPanel> 
< ListView Name =lvToursItemsSource ={Binding Tours}>
< ItemsControl.ItemTemplate>
< DataTemplate>
< Label Content ={Binding Name}/>
< / DataTemplate>
< /ItemsControl.ItemTemplate>
< / ListView>
< ListView Name =lvPartiesItemsSource ={Binding ElementName = lvTours,Path = SelectedItem.Parties}>
< ItemsControl.ItemTemplate>
< DataTemplate>
< Label Content ={Binding Name}/>
< / DataTemplate>
< /ItemsControl.ItemTemplate>
< / ListView>
< ListView Name =lvEquipesItemsSource ={Binding ElementName = lvParties,Path = SelectedItem.Equipes}>
< ItemsControl.ItemTemplate>
< DataTemplate>
< Label Content ={Binding Name}/>
< / DataTemplate>
< /ItemsControl.ItemTemplate>
< / ListView>
< / StackPanel>

然后我可以根据 SelectedItem 的菜单:

 < StackPanel Grid.Column =1> 
< local:StatistiquesTour DataContext ={Binding ElementName = lvTours,Path = SelectedItem}/>
< local:StatistiquesParties DataContext ={Binding ElementName = lvParties,Path = SelectedItem}/>
< local:StatistiquesEquipes DataContext ={Binding ElementName = lvEquipes,Path = SelectedItem}/>
< / StackPanel>

我的问题是如果我的菜单变成 Usercontrol ,我似乎不能在菜单和 ListView 之间的数据绑定(我命名为 lvMenu )里面的 MenuUserControl 。我尽可能简单地做这样的事情:

 < local:MenuUserControl x:Name =MenuToursDataContext ={Binding Tours}/> 
< local:MenuUserControl x:Name =MenuPartiesDataContext ={Binding ElementName = MenuTours.lvMenu,Path = SelectedItem}/>
< local:MenuUserControl x:Name =MenuEquipesDataContext ={Binding ElementName = MenuParties.lvMenu,Path = SelectedItem}/>

然后,上下文将以相同的方式可达:

 < local:StatistiquesTour DataContext ={Binding ElementName = MenuTours.lvMenu,Path = SelectedItem}/> 
< local:StatistiquesParties DataContext ={Binding ElementName = MenuParties.lvMenu,Path = SelectedItem}/>
...

lvMenu MenuUserControl 中的 ListView )的 ItemsSource ={Binding}将其绑定到上下文。



有谁有一个线索怎么做? (对我的英文抱歉)

解决方案

一种方法是用户控件中的DependencyProperty



另一个简单的方法是

为MenuTours用户控件创建一个ViewModel



在MenuTours中将ListViews selectedItem与ViewModel中的属性为TwoWayBinding



现在在主窗口中订阅MenuToursViewModel的propertyChanged事件

  MenuToursViewModel.PropertyChanged + = OnpropertyChanged; 

void OnPropertyChanged(Sender s,PropertyChangedEventArgs e)
{
if(e.PropertyName ==SelectedTour)
{
MenuPartiesViewModel.Items = SelectedTour.parties;
}
}

主要优点是此代码是可测试和可扩展的。



希望这有帮助。


I have a property Tours :

  • Tours is an ObservableCollection of class Tour
    • Each "Tour" has an ObservableCollection Parties of class Partie
      • Each Partie has an ObservableCollection Equipes of class Equipe

I have 3 menus :

  1. The first is is bond with the property Tours
  2. The second must be bond with the SelectedItem property of the first menu (so it has an ObservableCollection of class Partie)
  3. The third must be bond with the SelectedItem property of the second menu. (so it has an ObservableCollection of class Equipes)

Right now, this is the working code :

<StackPanel>
    <ListView Name="lvTours" ItemsSource="{Binding Tours}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding Name}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ListView>
    <ListView Name="lvParties" ItemsSource="{Binding ElementName=lvTours, Path=SelectedItem.Parties}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding Name}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ListView>
    <ListView Name="lvEquipes" ItemsSource="{Binding ElementName=lvParties, Path=SelectedItem.Equipes}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding Name}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ListView>
</StackPanel>

And then I can change the view of the context depending on the SelectedItem of the menus :

<StackPanel Grid.Column="1">
    <local:StatistiquesTour DataContext="{Binding ElementName=lvTours, Path=SelectedItem}" />
    <local:StatistiquesParties DataContext="{Binding ElementName=lvParties, Path=SelectedItem}" />
    <local:StatistiquesEquipes DataContext="{Binding ElementName=lvEquipes, Path=SelectedItem}" />            
</StackPanel>

My problem is if my menus becomes Usercontrol, I can't seem to make the databinding between the menu and the ListView (that I named : lvMenu) inside the MenuUserControl. I though it would be as simple as doing something like this :

<local:MenuUserControl x:Name="MenuTours" DataContext="{Binding Tours}" />
<local:MenuUserControl x:Name="MenuParties" DataContext="{Binding ElementName=MenuTours.lvMenu, Path=SelectedItem}" />
<local:MenuUserControl x:Name="MenuEquipes" DataContext="{Binding ElementName=MenuParties.lvMenu, Path=SelectedItem}" />

And then the context would be reachable the same way :

<local:StatistiquesTour DataContext="{Binding ElementName=MenuTours.lvMenu, Path=SelectedItem}" />
<local:StatistiquesParties DataContext="{Binding ElementName=MenuParties.lvMenu, Path=SelectedItem}" />
...

The lvMenu (the ListView) in MenuUserControl has its ItemsSource="{Binding}" to bind it to the context.

Does anyone have a clue how to do that? (sorry for my english)

解决方案

One way is DependencyProperty in the usercontrol

Another simple way is

Create a ViewModel for the MenuTours usercontrol

In MenuTours Bind ListViews selectedItem with a property in ViewModel as TwoWayBinding

Now in your main window subscribe to propertyChanged event of MenuToursViewModel

MenuToursViewModel.PropertyChanged += OnpropertyChanged;

    void OnPropertyChanged(Sender s, PropertyChangedEventArgs e)
    {
        if(e.PropertyName == "SelectedTour")
        {
           MenuPartiesViewModel.Items = SelectedTour.parties;
        }
    }

the main advantage is this code is testable and Scalable.

Hope this helps.

这篇关于UserControl菜单上的多级数据绑定依赖于彼此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆