I have trouble defining a trigger for TreeViewItems. I believe it is just some syntax problem, but I don't know what else to write...
This is the Trigger:
<DataTrigger Binding="{Binding Path=., Converter=IsNodeConverter}" Value="True">
<Setter Property="Focusable" Value="False"/>
</DataTrigger>
Since it is defined inside TreeView.ItemContainerStyle
, the DataContext
should be the contained item itself. The Item can either be of type Node
or Entry
and I want to trigger for all Items that are of type Node
. So I wrote a converter:
public class IsNodeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Node)
return true;
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Which returns true
if it gets a Node
as input and false
otherwise.
But in the part Binding="{Binding Path=., Converter=IsNodeConverter}"
the compiler complains: "IValueConverter cannot convert from string." (original: "Vom TypeConverter-Objekt für IValueConverter wird das Konvertieren aus einer Zeichenfolge nicht unterstützt.") I don't understand this at all: DataContext is an object of type Entry
or Node
, and Binding Path=.
should keep it that way. So what is the problem? What string is the compiler talking about? How do I correct this so that the compiler does not complain?
Here is the full code of the TreeView for reference. The collection ´AllNodesAndEntries´ is an ObservableCollection<object>
that contains both Node
s and Entry
s.
<TreeView ItemsSource="{Binding AllNodesAndEntries}">
<TreeView.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type usrLibVM:Node}">
<TextBlock Text="{Binding Name}" Background="LightBlue"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type usrLibVM:Entry}">
<TextBlock Text="{Binding Name}" Background="LightSalmon"/>
</DataTemplate>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=., Converter=IsNodeConverter}" Value="True">
<Setter Property="Focusable" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>