Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...
Posted

1 solution

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,
XML
<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>

For RadioButton Child Item,
XML
<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>

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,
C#
YourTreeView.Items.Add(RadioButtonChildObject);
YourTreeView.Items.Add(CheckBoxChildObject);
 
Share this answer
 
v3
Comments
TurgayTürk 7-Oct-15 4:05am    
Thank you for your answer. I had to change my approach but it is exactly what i was looking for!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900