0
votes

I want to add the check box for treeview of observable collection. the following code add the check box for all the parent and children nodes. I need only for the parent node Is there any way to achieve this...

<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

2 Answers

0
votes

There are tons of ways you can achieve this.

One of them:

You can go to the ViewModel for the TestAutomationClient:TestArtifact, and add the bool property here:

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

You should update it when the entire collection has changed (if this is your case), i.e. you should make RaisePropertyChanged("IsEmpty"). In case you are using some kind of ICollectionView - you do not need that property on your 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>
0
votes

You need a DataTrigger with a Converter. The Trigger should look like below,

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

The whole TreeViewItem is being send to the Converter. The Converter will look for a Visual Parent. If it finds a Parent as TreeView, it is the first level. If it finds TreeViewItem, then it is sub levels.

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();
    }
}

The extension method FindAncestor<T> is available in this link.