babboon
babboon

Reputation: 793

xaml c# treeview checkboxex uncheck all others

I have this xaml. And I need to uncheck all other checkboxes where one is checked. I other words to allow to check only one. I add TreeViewItems on a runtime.

<TreeView Name="treeView_max" >
    <TreeView.Resources>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" >
                            <CheckBox Name="chk" Margin="2" Tag="{Binding}" Checked="checkBox_Checked">
                            </CheckBox>
                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TreeView.Resources>
</TreeView>

Adding TreeViewItems at runtime:

foreach (Genesyslab.Desktop.Modules.Core.Model.BusinessAttributes.IDispositionCodeValue item in listOfDispositionCodeValueItemsControl.Items)
{
    TreeViewItem newChild2 = new TreeViewItem();
    newChild2.Header = item.DisplayName.Remove(0,item.DisplayName.IndexOf("-") + 1);
    treeView_max.Items.Add(newChild2);..........`

and

private void checkBox_Checked(object sender, RoutedEventArgs e)
{
    try
    {
        //uncheck all checkboxes  except selected one         
    }
    catch (Exception es)
    {
        MessageBox.Show(es.ToString());
    }
}

Upvotes: 0

Views: 179

Answers (1)

Grant Winney
Grant Winney

Reputation: 66449

You can use RadioButton controls that belong to the same group instead, which will get you the behavior of only one option being able to be selected at a time.

Then edit the control template to display CheckBox controls in place of those RadioButton's, and bind the IsChecked property of each CheckBox to its parent RadioButton. Now when you "check" a CheckBox, all other CheckBox controls will become unchecked.

<TreeView Name="treeView_max" >
    <TreeView.Resources>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" >
                            <RadioButton Name="chk" Margin="2" Tag="{Binding}" GroupName="SomeGroup">
                                <RadioButton.Template>
                                    <ControlTemplate>
                                        <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=RadioButton}}" />
                                    </ControlTemplate>
                                </RadioButton.Template>
                            </RadioButton>
                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TreeView.Resources>
</TreeView>

Be careful about where you use this. Users are used to seeing RadioButton's when they're only able to select one option, and CheckBox's where they can select multiple options.

Upvotes: 1

Related Questions