如何在TreeView中混合/组合复选框和单选按钮? [英] How to mix / combine checkboxes and radiobuttons in a TreeView?

查看:63
本文介绍了如何在TreeView中混合/组合复选框和单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的xaml列出了树形视图中带有复选框的分层列表,到目前为止它工作得很好。但我需要的是确定我的子属性的复选框和单选按钮之间的控制。在某些情况下,用户可以选择多选(复选框),在其他情况下,他必须在它们之间做出决定(单选按钮)。如何在树视图中组合radiobutton和checkbox控件?



XAML:

I use the below xaml to list hierarchical lists with checkboxes in a treeview and it works fantastic so far. But what i need is to determine the Control between a checkbox and a radiobutton for my child property. In some cases the user has the option for multible selection (checkbox) and in other cases he has to decide between them (radiobutton). How can i combine the radiobutton and checkbox control in a treeview?

XAML :

<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Child}">
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding ImgPath}" VerticalAlignment="Center"  Margin="2" />
            <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" VerticalAlignment="Center" Margin="2" />
            <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Margin="2" />
        </StackPanel>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>





提前谢谢...



Thank you in advance...

推荐答案

只需用 DataType 定义两个 HierarchicalDataTemplate 即可。一个用于具有CheckBox控件的Child,另一个用于具有RadioButton的Child。比如,



对于CheckBox子项,

Hi, Just define two HierarchicalDataTemplate with DataType. One is for the Child having CheckBox Control, and Other one is for the Child having RadioButton. Like,

For CheckBox Child Item,
<TreeView.ItemTemplate>
    <HierarchicalDataTemplate DataType="{x:Type local:CheckBoxChild}" >
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding ImgPath}" VerticalAlignment="Center"  Margin="2" />
            <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" VerticalAlignment="Center" Margin="2" />
            <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Margin="2" />
        </StackPanel>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>



对于RadioButton儿童项目,


For RadioButton Child Item,

<TreeView.ItemTemplate>
    <HierarchicalDataTemplate DataType="{x:Type local:RadioButtonChild}" >
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding ImgPath}" VerticalAlignment="Center"  Margin="2" />
            <RadioButton GroupName="Selection" Name="{Binding Option1Name}" IsChecked="{Binding IsOption1Checked, Mode=TwoWay}" VerticalAlignment="Center" Margin="2" />
          <RadioButton GroupName="Selection" Name="{Binding Option2Name}" IsChecked="{Binding IsOption2Checked, Mode=TwoWay}" VerticalAlignment="Center" Margin="2" />                              
        </StackPanel>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>



这里本地指的是你的应用程序的名称空间。

而且,只需为CheckBox Child Item和RadioButton ChildItem定义两个类,然后基于这些类,您可以创建对象,并且可以将这些对象添加到树视图中,例如,


Here local refers your application's namespace.
And, Just define two classes for CheckBox Child Item and RadioButton ChildItem, then based on these classes you can create objects and you can add those objects to your treeview like,

YourTreeView.Items.Add(RadioButtonChildObject);
YourTreeView.Items.Add(CheckBoxChildObject);


这篇关于如何在TreeView中混合/组合复选框和单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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