TreeView与单选按钮

时间:2014-01-23 14:06:21

标签: c# wpf xaml treeview

我需要在WPF项目中创建动态节点创建TreeView。 TreeView节点有一个条件。所有叶子必须包含Radiobuttons,其工作逻辑与RadioButtons组相同。

我有这个XAML代码,它描述了树视图数据模板:

<HierarchicalDataTemplate x:Key="sko_ver_hdt">
        <RadioButton Margin="0,0,10,0" Content="{Binding Version}"/>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate x:Key="sko_hdt"
                              ItemTemplate="{StaticResource sko_ver_hdt}"
                              ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Name}"/>
    </HierarchicalDataTemplate>

我尝试使用CollectionViewSource对TreeView的节点进行分组,如下所示:

<CollectionViewSource x:Key="cvs" Source="{Binding SKOs}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Version"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

但没有任何反应。

我有一个描述树视图数据的模型:

internal class SKOVM
{
    public SKOVM(DBCommunication.SKO sko)
    {
        Name = sko.Name;
        Version = sko.VersionCode;
        Id = sko.Id;
        Children = new ObservableCollection<SKOVM>(sko.SKO1.Select(x => new SKOVM(x)));
        sko.SKOSystemInformationReference.Load();
        if (sko.SKOSystemInformation != null)
            Version = String.Format("{0} / {1}", Version, sko.SKOSystemInformation.Designer);
    }

    public long Id { get; set; }

    public string Name { get; set; }
    public string Version { get; set; }

    public ObservableCollection<SKOVM> Children { get; set; }
}

我可以如何实现分组radiobuttons行为逻辑到我的treeview叶节点?

提前致谢。

1 个答案:

答案 0 :(得分:3)

您是否尝试过使用RadioButton.GroupName property?:

<HierarchicalDataTemplate x:Key="sko_ver_hdt">
    <RadioButton Content="{Binding Version}" GroupName="{Binding GroupName}" />
</HierarchicalDataTemplate>

此属性获取或设置指定哪些RadioButton控件互斥的名称。因此,您需要子项类中的属性,其中每个组的子项具有相同的值。通过这种方式,您可以将该值绑定到GroupName属性,并且每个子项的RadioButton应该在各自的组中工作。