首页 > 解决方案 > 带有复选框的 Wpf TreeView

问题描述

我需要将“全选”添加到我的 wpf 应用程序中,我尝试做一个简单的绑定,但它不起作用。

有人可以建议,执行简单的绑定或其他严格的解决方案吗?

这是我的树视图 xaml :

 <TreeView Name="CurrentTreeView" Margin="5" BorderBrush="Transparent" BorderThickness="0"
                      ScrollViewer.VerticalScrollBarVisibility="Auto">
                <TreeView.ItemContainerStyle>
                    <Style TargetType="TreeViewItem">
                        <Setter Property="IsSelected" Value="{Binding IsSelected}" />
                        <Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
                        <Setter Property="HeaderTemplate" >
                            <Setter.Value>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">

                                        <CheckBox                                          
                                            Focusable="False" 
                                            Checked="CheckedItemCurreuntSession"
                                            Unchecked="UnCheckedItemCurreuntSession"
                                            Name="treeChk"
                                            Tag="{Binding Path=ChName}"  
                                            Style="{StaticResource CheckBoxStyle1}"
                                            IsChecked="{Binding Path=IsChecked}"
                                            VerticalAlignment="Center"/>

                                        <TextBlock VerticalAlignment="Center" Text="{Binding}" 
                                                   Margin="5"
                                                       TextOptions.TextFormattingMode="Display"
                                                       TextOptions.TextHintingMode="Auto"
                                                        />
                                    </StackPanel>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TreeView.ItemContainerStyle>
            </TreeView>

标签: c#wpf

解决方案


<Style x:Key="CheckBoxListStyle" TargetType="{x:Type ListBox}">
    <Setter Property="SelectionMode" Value="Multiple"/>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Margin" Value="2"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <CheckBox Focusable="False" IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent} }">
                                <ContentPresenter/>
                            </CheckBox>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

推荐阅读