Using Path =. and the converter inside the binding

I find it difficult to define a trigger for TreeViewItems. I believe this is just a syntax issue, 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 internally TreeView.ItemContainerStyle, it DataContextmust be content. An element can be of type Nodeor Entry, and I want to call for all elements of the type Node. Therefore, 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();
    }
}

That returns true, if he receives input Nodeand falseotherwise.

Binding="{Binding Path=., Converter=IsNodeConverter}" : "IValueConverter ". (: "Vom TypeConverter-Objekt für IValueConverter wird das Konvertieren aus einer Zeichenfolge nicht unterstützt." ) : DataContext - Entry Node, Binding Path=. . , ? ? , ?

TreeView . 'AllNodesAndEntries ' ObservableCollection<object>, Node, 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>
0
2

, , ResourceDictionary, StaticResource:

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

:

Binding="{Binding Converter={StaticResource IsNodeConverter}}" 
+2

Value WPF :

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

Source: https://habr.com/ru/post/1657080/


All Articles