使用Path =。和转换器内部绑定 [英] Using Path=. and Converter inside Binding
问题描述
我无法为TreeViewItem定义触发器。我相信这只是一些语法问题,但我不知道还有什么要写的...
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...
这是触发器:
<DataTrigger Binding="{Binding Path=., Converter=IsNodeConverter}" Value="True">
<Setter Property="Focusable" Value="False"/>
</DataTrigger>
由于它在 TreeView.ItemContainerStyle
, DataContext
应该是包含的项目本身。 Item可以是类型 Node
或 Entry
,我想触发所有类型为$的项目c $ c> Node 。所以我写了一个转换器:
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();
}
}
哪些返回 true
如果它得到一个
Node
作为输入, false
否则。
Which returns true
if it gets a Node
as input and false
otherwise.
但是在部分 Binding ={Binding Path =。,Converter = IsNodeConverter}
编译器抱怨:IValueConverter无法从字符串转换。 (原文:Vom TypeConverter-ObjektfürIValueConverter wird das Konvertieren aus einer Zeichenfolge nichtunterstützt)我不明白这一点:DataContext是一个类型为 Entry
的对象或 Node
和 Binding Path =。
应该保持这样。那么问题是什么呢?编译器讲什么字符串?如何纠正这一点,以便编译器不会抱怨?
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?
以下是TreeView的完整代码以供参考。集合'AllNodesAndEntries'是一个 ObservableCollection< object>
,其中包含 Node
和条目
s。
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>
推荐答案
您的转换器肯定在ResourceDictionary中声明,所以它应该被引用为StaticResource:
Your converter is certainly declared in a ResourceDictionary, so it should be referenced as StaticResource:
Binding="{Binding Path=., Converter={StaticResource IsNodeConverter}}"
或更短:
Binding="{Binding Converter={StaticResource IsNodeConverter}}"
这篇关于使用Path =。和转换器内部绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!