C# 如何获取WPF转换器的调用者?
我试图在Convert函数中获取调用转换器的元素 原因是,我已经为TreeViewItems创建了一个样式,并希望将BackgroundColor绑定到内容(如果有子项或没有子项)。因此,我需要一个转换器,他需要知道对应项包含什么,因此在我看来,他现在是呼叫者是必要的 以下是我的风格:C# 如何获取WPF转换器的调用者?,c#,wpf,treeview,binding,C#,Wpf,Treeview,Binding,我试图在Convert函数中获取调用转换器的元素 原因是,我已经为TreeViewItems创建了一个样式,并希望将BackgroundColor绑定到内容(如果有子项或没有子项)。因此,我需要一个转换器,他需要知道对应项包含什么,因此在我看来,他现在是呼叫者是必要的 以下是我的风格: <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="IsExpanded" Value="{
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="1,0,0,0"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<StackPanel>
<Border Name="Bd" Background="{TemplateBinding Background, Converter={StaticResource NodeBackgroundConverter}}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<Grid Margin="{Binding Converter={StaticResource lengthConverter},
RelativeSource={RelativeSource TemplatedParent}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="19" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ToggleButton x:Name="Expander"
Style="{StaticResource ExpandCollapseToggleStyle}"
IsChecked="{Binding Path=IsExpanded,
RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press"/>
<ContentPresenter x:Name="PART_Header"
Grid.Column="1"
ContentSource="Header"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
</Grid>
</Border>
<ItemsPresenter x:Name="ItemsHost"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
现在的问题是如何使用“NodeBackgroundConverter”实现这一点
Thx为什么不使用StyleSelector类 1-创建2个样式 1.1-一个用于simpel TreeView项目 1.2-一个用于具有子项的TreeView项目 2-然后创建从StyleSelector继承的类 3-覆盖SelectStyle方法
public class SeparatorTabStyleSelector : StyleSelector
{
#region " StyleSelector Implementation "
public override Style SelectStyle(
object item,
DependencyObject container)
{
object data = item as 'Your Bindable Object';
if ('Your Condition Based upon item object')
{
return (Style)((FrameworkElement)container).FindResource("Style1");
}
else if ('If Condition is not true Based upon item object')
{
return (Style)((FrameworkElement)container).FindResource("Style2");
}
return base.SelectStyle(item, container);
}
#endregion " StyleSelector Implementation "
}
尝试使用绑定到HasItems属性的DataTrigger
<DataTrigger Property="{Binding Path=HasItems}" Value="True">
<Setter Property="DataTemplate" Value="{StaticResource subitemtemplate}" />
</DataTrigger>
在样式中,将DataTemplate设置为另一个模板,当TreeView有子项时,该模板将被替换。如果三个级别中有两个以上的级别,则这将不起作用,因为需要使用不同的颜色。是的,将触发器绑定到布尔属性的选项有限,但您也可以轻松地绑定到datacontext上的属性以进行此确定,例如枚举。另外,你的问题是:是否有子项