如何将2个列表框绑定到1个可观察集合

时间:2015-02-13 02:24:14

标签: c# wpf xaml listbox

我需要2节原则和机智。 原则可以有很多机智主义。

在我的xaml中有2个列表框(principleListBox和mecanismListBox),我想选择一个原则,然后mecanismListBox应该显示所选原理的所有机制。当我添加新的mecanims或原则时,所有列表框都应该更新。

模型

public class Principle : INotifyPropertyChanged
{
    public virtual Guid PrincipleId { get; set; }
    public virtual string Name { get; set; }
    public virtual ObservableCollection<Mecanism> Mecanisms { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}


public class Mecanism : INotifyPropertyChanged
{
    public virtual Guid MecanismId { get; set; }
    public virtual string Name { get; set; }
    public virtual Guid PrincipleId { get; set; }

    public virtual Principle Principle { get; set; }

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

MainView.xaml.cs

public partial class MainWindow : Window
{
    private ObservableCollection<Principle> _principles = new ObservableCollection<Principle>();
    private IContext _context;
    public MainWindow()
    {
        InitializeComponent();
        _context = new BinaryContext(AppDomain.CurrentDomain.BaseDirectory);
        foreach (Principle principle in _context.GetAllPrinciples())
        {
            _principles.Add(principle);
        }
        PrinciplesListBox.ItemsSource = _principles;
        PrinciplesListBox.DisplayMemberPath = "Name";
        PrinciplesListBox.SelectedValuePath = "PrincipleId";
    }

    private void CreatePrinciple_Click(object sender, RoutedEventArgs e)
    {
        Principle principle = new Principle
        {
            Name = "principle test",
            Mecanisms = new ObservableCollection<Mecanism>
            {
                new Mecanism{Name = "mecanism test"}
            }
        };
        _principles.Add(principle);
    }
}

解决 我使用了更改列表框事件

    private void entryListBox_Changed(object sender, SelectionChangedEventArgs e)
    {
        if (PrinciplesListBox.SelectedIndex != -1)
        {
               //update combo of mechanism.....
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以通过使用某种“支持字段”来解决此问题。根据您当前选择的原则。

在您的模型中,添加一个新的实例成员,并将其命名为&#39; SelectedPrinciple&#39;。

然后将原则列表框中的SelectedValue属性设置为SelectedPrinciple。

<ListBox Name="lbPrinciples" SelectedValue="{Binding SelectedPrinciple}"/>

接下来,将机制列表框中的ItemsSource属性设置为SelectedPrinciple.Mechanisms。

<ListBox Name="lbMechanisms" ItemsSource={Binding SelectedPrinciple.Mechanisms}"/>

最后,最重要的是,将您的Principle类型的Mechanisms属性更改为此

private ObservableCollection<Mechanism> _mechanisms;
public ObservableCollection<Mechanism> Mechanisms
{
    get { return _mechanisms; }
    set
    {
        _mechanisms = value;
        OnPropertyChanged("Mechanisms");
    }    
}

现在,只要您的选择更改了主要项目,机制列表框将显示所选原则的该集合的所有子项目。

干杯。