Data binding 组合框selectedItem影响另一个组合框

Data binding 组合框selectedItem影响另一个组合框,data-binding,windows-store-apps,Data Binding,Windows Store Apps,我有一个关于数据绑定的问题,我真的很难理解。我的扩展启动屏幕上有两个组合框。我想要实现的是,当您从第一个组合框中选择一个项目时,第二个组合框中的项目应该会更改。请参阅下面的代码 首先,让我试着解释一下我的数据是什么样子的,以及我面临的问题是什么 收藏 A AA AB 交流电 B BA BB C CA 第一个组合框应显示A、B和C作为项目。现在假设您选择了A,组合框2应该显示AA、AB和AC作为项目。我的问题是ComboBox 2只显示AA,而不是所有3项 我的名为MainVi

我有一个关于数据绑定的问题,我真的很难理解。我的扩展启动屏幕上有两个组合框。我想要实现的是,当您从第一个组合框中选择一个项目时,第二个组合框中的项目应该会更改。请参阅下面的代码

首先,让我试着解释一下我的数据是什么样子的,以及我面临的问题是什么

  • 收藏
    • A
      • AA
      • AB
      • 交流电
    • B
      • BA
      • BB
    • C
      • CA
第一个组合框应显示A、B和C作为项目。现在假设您选择了A,组合框2应该显示AA、AB和AC作为项目。我的问题是ComboBox 2只显示AA,而不是所有3项

我的名为MainViewModel的ViewModel如下所示:-

    public class ItemViewModel : INotifyPropertyChanged
{

    private string _befattning;
    public string Befattning
    {
        get
        {
            return _befattning;
        }
        set
        {
            if (value != _befattning)
            {
                _befattning = value;
                NotifyPropertyChanged("Befattning");
            }
        }
    }

    private string _befattning2;
    public string Befattning2
    {
        get
        {
            return _befattning2;
        }
        set
        {
            if (value != _befattning2)
            {
                _befattning2 = value;
                NotifyPropertyChanged("Befattning2");
            }
        }
    }

    private string _befattning3;
    public string Befattning3
    {
        get
        {
            return _befattning3;
        }
        set
        {
            if (value != _befattning3)
            {
                _befattning3 = value;
                NotifyPropertyChanged("Befattning3");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        //NotifyPropertyChanged Code
    }
}



public class MainViewModelGroups : INotifyPropertyChanged
{
    public MainViewModelGroups(String enhet)
    {
        this._enhetsNamn = enhet;
    }

    private string _enhetsNamn;
    public string EnhetsNamn
    {
        get { return _enhetsNamn; }
    }

    private string _selectedItem;
    public string SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            if (value != _selectedItem)
            {
                _selectedItem = value;
                NotifyPropertyChanged("SelectedItem");
            }
        }
    }

    private ObservableCollection<ItemViewModel> _items = new ObservableCollection<ItemViewModel>();
    public ObservableCollection<ItemViewModel> Items
    {
        get
        {
            return this._items;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        //NotifyPropertyChangedCode
    }
}

public sealed class MainViewModel
{
    private static MainViewModel _mainViewModel = new MainViewModel();

    private ObservableCollection<MainViewModelGroups> _collection = new ObservableCollection<MainViewModelGroups>();
    public ObservableCollection<MainViewModelGroups> Collection
    {
        get { return this._collection; }
    }

    public MainViewModel()
    {
        var enhet1 = new MainViewModelGroups("Akutmottagning");
        enhet1.Items.Add(new ItemViewModel() { Befattning = "Ledningsansvarig sjuksköterska" });
        Collection.Add(enhet1);
    }
公共类ItemViewModel:INotifyPropertyChanged
{
私人字符串(beattning);;
公共字符串beattning
{
得到
{
归来!;
}
设置
{
如果(值!=\u beattning)
{
_beattning=价值;
通知财产变更(“beattning”);
}
}
}
专用字符串_beattning2;
公共字符串beattning 2
{
得到
{
返回2;
}
设置
{
如果(值!=\u beattning 2)
{
_befatting2=值;
NotifyPropertyChanged(“Befatting2”);
}
}
}
专用字符串_beattning3;
公共字符串beattning 3
{
得到
{
返回3之前;
}
设置
{
如果(值!=\u beattning 3)
{
_befatting3=值;
NotifyPropertyChanged(“Befatting3”);
}
}
}
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串propertyName)
{
//NotifyPropertyChanged代码
}
}
公共类MainViewModelGroups:INotifyPropertyChanged
{
公共主视图模型组(字符串enhet)
{
这个。_enhetsnam=enhet;
}
私有字符串_enhetsnam;
公共字符串EnhetsNamn
{
获取{return}
}
私有字符串_selectedItem;
公共字符串SelectedItem
{
获取{return\u selectedItem;}
设置
{
如果(值!=\u选择编辑项)
{
_选择editem=值;
NotifyPropertyChanged(“SelectedItem”);
}
}
}
私有ObservableCollection_items=新ObservableCollection();
公共可观测收集项目
{
得到
{
归还此物品;
}
}
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串propertyName)
{
//NotifyPropertyChangedCode
}
}
公共密封类主视图模型
{
私有静态MainViewModel _MainViewModel=新的MainViewModel();
私有ObservableCollection_collection=新ObservableCollection();
公开收集
{
获取{返回此。\集合;}
}
公共主视图模型()
{
var enhet1=新的主视图模型组(“Akutmottagning”);
添加(新的ItemViewModel(){Befattning=“Ledningsansvarig sjuksköterska”});
集合。添加(enhet1);
}
我的XAML代码如下所示

<ComboBox x:Name="EnhetLista" 
              ItemsSource="{Binding Collection}"
              SelectedItem="{Binding SelectedItem, Mode=OneWay}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding EnhetsNamn}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    <ComboBox x:Name="BefattningsLista"
              DataContext="{Binding ElementName=EnhetLista, Path=SelectedItem, Mode=OneWay}"
              ItemsSource="{Binding Path=Items, Mode=OneWay}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>


有人能帮我解释一下我的代码有什么问题以及如何实现我的目标吗?

在viewmodel中添加第二个ObservableCollection,然后根据所选项目进行更改

public sealed class MainViewModel
{
    private static MainViewModel _mainViewModel = new MainViewModel();

    private ObservableCollection<MainViewModelGroups> _collection = new ObservableCollection<MainViewModelGroups>();
    public ObservableCollection<MainViewModelGroups> Collection
    {
        get { return this._collection; }
    }

    public ObservableCollection<ItemViewModel> Items
    {
        get { return this._items; }
        set { this._items = value; OnPropertyChanged("Items"); }
    }

    public MainViewModelGroups SelectedGroup
    {
        get { return this._selectedGroup; }
        set { this._selectedGroup = value; Items = value.Items; }
    }

    public MainViewModel()
    {
        var enhet1 = new MainViewModelGroups("Akutmottagning");
        enhet1.Items.Add(new ItemViewModel() { Befattning = "Ledningsansvarig sjuksköterska" });
        Collection.Add(enhet1);
    }
}
公共密封类MainViewModel
{
私有静态MainViewModel _MainViewModel=新的MainViewModel();
私有ObservableCollection_collection=新ObservableCollection();
公开收集
{
获取{返回此。\集合;}
}
公共可观测收集项目
{
获取{返回此。_items;}
设置{this._items=value;OnPropertyChanged(“items”);}
}
公共主视图模型组已选择组
{
获取{返回此。\u selectedGroup;}
设置{this.\u selectedGroup=value;Items=value.Items;}
}
公共主视图模型()
{
var enhet1=新的主视图模型组(“Akutmottagning”);
添加(新的ItemViewModel(){Befattning=“Ledningsansvarig sjuksköterska”});
集合。添加(enhet1);
}
}
您的Xaml将更改为:

<ComboBox x:Name="EnhetLista" 
          ItemsSource="{Binding Collection}"
          SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding EnhetsNamn}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
<ComboBox x:Name="BefattningsLista"
          ItemsSource="{Binding Path=Items}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>


您的viewmodel可能需要实现INotifyPropertyChanged。但可能不需要,因为ObservableCollection可能会为您解决这一问题。

在查看了它和您的实现之后,我认为您的代码存在的问题是,我认为您正在尝试使用listbox中的绑定设置基础组的SelectedItem,并且不起作用。我发布的代码应该可以帮助你。谢谢你的回答,问题是我在集合树中的最后两个项目被添加为一个项目。这就是为什么我修改了我的ObservableCollection以单独添加这些项目。我忘了更新答案。糟糕。如果有人感兴趣,我可以添加代码,这样每个人都会受益谢谢你,内特。