Using Path=. and Converter inside Binding

 ̄綄美尐妖づ 提交于 2019-12-12 18:00:20

问题


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>

回答1:


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



回答2:


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>


来源:https://stackoverflow.com/questions/39955455/using-path-and-converter-inside-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!