【问题标题】:xaml c# treeview checkboxex uncheck all othersxaml c#treeview checkboxex取消选中所有其他
【发布时间】:2014-11-22 20:15:19
【问题描述】:

我有这个 xaml。我需要取消选中所有其他选中的复选框。换句话说,只允许检查一个。我在运行时添加 TreeViewItems。

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

在运行时添加 TreeViewItems:

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);..........`

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

【问题讨论】:

    标签: c# xaml checkbox treeview


    【解决方案1】:

    您可以改用属于同一组的RadioButton 控件,这将使您获得一次只能选择一个选项的行为。

    然后编辑控件模板以显示CheckBox 控件来代替那些RadioButton 的控件,并将每个CheckBoxIsChecked 属性绑定到其父RadioButton。现在,当您“选中”CheckBox 时,所有其他 CheckBox 控件都将变为未选中状态。

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

    注意你在哪里使用它。用户习惯于看到RadioButton 只能选择一个选项,而CheckBox 则可以选择多个选项。

    【讨论】:

      猜你喜欢
      • 2012-06-16
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 2014-04-29
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多