【问题标题】:How to get the caller of a WPF Converter?如何获取 WPF 转换器的调用者?
【发布时间】:2010-10-12 10:54:10
【问题描述】:

我尝试在它的 Convert 函数中获取调用转换器的元素。

原因是,我有 TreeViewItems 的样式,并且想要将 BackgroundColor 绑定到内容(如果有子项或没有子项)。因此我需要转换器,他需要知道对应的项目包含什么,因此在我看来,他现在是他的调用者。

这是我的风格:

        <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”来做到这一点。

谢谢

【问题讨论】:

    标签: c# wpf treeview binding


    【解决方案1】:

    你为什么不使用 StyleSelector 类

    1- 创建 2 个样式

    1.1 - 一个用于 simpel treeviewitem 1.2 - 一个用于具有子项的 treeviewitem

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

    【讨论】:

      【解决方案2】:

      尝试使用绑定到 HasItems 属性的 DataTrigger。

      <DataTrigger Property="{Binding Path=HasItems}" Value="True">
        <Setter Property="DataTemplate" Value="{StaticResource subitemtemplate}" />
      </DataTrigger>
      

      在样式中将 DataTemplate 设置为另一个模板,当 TreeView 有子项时,模板将被替换

      【讨论】:

      • 好吧,如果我在三个级别中有两个以上需要不同的颜色,那就不行了。
      • 是的,将触发器绑定到布尔属性的选项有限,但您可以轻松地绑定到 datacontext 上的属性来做出此决定,例如枚举。另外,您的问题确实说明了:是否有子项
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      相关资源
      最近更新 更多