Using Path=. and Converter inside Binding
Asked Answered
B

2

2

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 Nodes and Entrys.

<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>
Backstage answered 10/10, 2016 at 9:41 Comment(0)
C
3

Your converter is certainly declared in a ResourceDictionary, so it should be referenced as StaticResource:

Binding="{Binding Path=., Converter={StaticResource IsNodeConverter}}" 

or shorter:

Binding="{Binding Converter={StaticResource IsNodeConverter}}" 
Craniometer answered 10/10, 2016 at 9:48 Comment(1)
In fact, no. It is not defined in a ResourceDictionary. But thanks to your hint regarding referencing the source, I was able to solve it with this answer: https://mcmap.net/q/573497/-using-value-converters-in-wpf-without-having-to-define-them-as-resources-firstBackstage
B
0

Based on answer in thread Using Value Converters in WPF without having to define them as resources first :

<DataTrigger Value="False">
    <DataTrigger.Binding>
        <Binding> <!-- <Binding Path="."> is possible but not necessary -->
            <Binding.Converter>
                <converterNamespace:IsNodeConverter/>
            </Binding.Converter>
        </Binding>
    </DataTrigger.Binding>
    <Setter Property="Focusable" Value="False"/>
</DataTrigger>
Backstage answered 10/10, 2016 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.