在caliburn.micro中绑定和访问动态组合框/列表框集合

时间:2011-11-22 12:16:11

标签: binding combobox caliburn.micro

我正在使用Caliburn Micro MVVM。我想根据通用树集合进行类别选择usercontrol,包括一些动态组合框(或列表框)。用户必须从类别树中选择任何叶节点,因此只要所选节点下面有子节点,新集合就会一直显示。深度可能会有所不同。

我希望它看起来像这样:http://i.imgur.com/c2uzv.png

......到目前为止它看起来像这样:

CategorySelectorModel.cs

public BindableCollection<BindableCollection<Category>> Comboboxes { get; set; }

CategorySelector.xaml

<ItemsControl x:Name="Comboboxes">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding}" DisplayMemberPath="Name"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

所以我的问题是:是否可以为每个创建的组合框指定一个事件并访问其SelectedItem属性?

1 个答案:

答案 0 :(得分:1)

比我想象的容易。从这一点来看,我的问题非常不幸。我从这开始:

<ComboBox ItemsSource="{Binding}" DisplayMemberPath="Name" cal:Message.Attach="CategoryChanged($this.SelectedItem)"/>

我的类别树的每个节点都有一个Depth属性。由于最后选择的元素的深度与集合的数量有关,因此我只使用此属性在任何所选项目发生更改时删除所有不必要的集合。

public void CategoryChanged(object selected)
{
    int depth = 0;
    var newcombobox = new BindableCollection<Category>();
    foreach (var node in _tree.All.Nodes)
    {
        if (node.Data.Equals(selected))
        {
            foreach (var category in node.DirectChildren.Values)
            {
                newcombobox.Add(category);
            }
            depth = node.Depth;
        }
    }
    if (newcombobox.Count > 0)
    {
        Comboboxes.Add(newcombobox);
    }
    RemoveFollowing(Comboboxes, depth);
}
相关问题