仅为Treeview父节点添加复选框Wpf

时间:2014-12-15 06:48:15

标签: wpf treeview

我想为可观察集合的树视图添加复选框。以下代码添加所有父节点和子节点的复选框。我只需要父节点有没有办法实现这个...

<HierarchicalDataTemplate x:Key="LogFolderExplorer" DataType="{x:Type TestAutomationClient:TestArtifact}" ItemsSource="{Binding Children}">
        <StackPanel Orientation="Horizontal" MinWidth="200">
            <CheckBox
            Focusable="False" 
            IsChecked="{Binding IsChecked}" 
            VerticalAlignment="Center"
            />
            <TextBlock Text="{Binding Name}" FontSize="14"/>
        </StackPanel>
    </HierarchicalDataTemplate>

2 个答案:

答案 0 :(得分:0)

你需要一个带转换器的DataTrigger。触发器应如下所示,

<HierarchicalDataTemplate ItemsSource="{Binding SubItems}">
    <StackPanel Orientation="Horizontal">
        <CheckBox x:Name="chk" Visibility="Collapsed"/>
        <TextBlock Text="{Binding Name}"
                    VerticalAlignment="Center"
                    Margin="3 0" />
    </StackPanel>
    <HierarchicalDataTemplate.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource TreeLevelFinder}}" 
                     Value="True">
            <Setter Property="Visibility"
                    TargetName="checkBox"
                    Value="Visible" />
        </DataTrigger>
    </HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>

整个TreeViewItem正被发送到Converter。转换器将查找Visual Parent。如果它找到Parent作为TreeView,则它是第一级。如果它找到TreeViewItem,那么它就是子级别。

public class TreeLevelFinder : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is FrameworkElement)
        {
            var element = value as FrameworkElement;
            var treeItem = element.TemplatedParent as TreeViewItem;
            var parent = treeItem.FindAncestor<ItemsControl>();
            if (parent is TreeView)
            {
                return true;
            }
        }
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

this link中提供了扩展方法FindAncestor<T>

答案 1 :(得分:0)

有很多方法可以实现这一目标。

其中一个:

您可以转到TestAutomationClient:TestArtifact的ViewModel,并在此处添加bool属性:

public bool IsEmpty { get { return !this.Children.Any() } }

当整个集合发生变化时(如果是这种情况),你应该更新它,即你应该制作RaisePropertyChanged(&#34; IsEmpty&#34;)。如果您使用某种ICollectionView - 您的ViewModel上不需要该属性。

<HierarchicalDataTemplate x:Key="LogFolderExplorer" 
                          DataType="{x:Type TestAutomationClient:TestArtifact}"
                          ItemsSource="{Binding Children}">
    <StackPanel Orientation="Horizontal" MinWidth="200">
        <CheckBox Focusable="False" 
                  IsChecked="{Binding IsChecked}" 
                  VerticalAlignment="Center">
            <CheckBox.Style>
                <Style TargetType="CheckBox">
                    <Setter Property="Visibility" Value="Visible" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsEmpty}" Value="True">
                            <Setter Property="Visibility" Value="Collapsed" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </CheckBox.Style>
        </CheckBox>
        <TextBlock Text="{Binding Name}" FontSize="14"/>
    </StackPanel>
</HierarchicalDataTemplate>