开发者

How to get the caller of a WPF Converter?

I try to get the element which calls a converter in it's Convert function.

The reason is, that I've got a style for TreeViewItems, and want to Bind the BackgroundColor to the Content(If there are subitems or not). Therefore I need the Converter, who needs to know what the correspondenting Item contains, and in my opinion therefore it's needet that he nows his caller.

Here my Style:

        <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}"&开发者_StackOverflow社区gt;
                    <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>

The Question is now how to do this, using the "NodeBackgroundConverter".

Thx


Why don't you use StyleSelector Class

1- Create 2 styles

1.1 - One for simpel treeviewitem 1.2 - One for treeviewitem having subitems

2-than create a class which is inherited from StyleSelector

3- Override SelectStyle method

 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 "

}


Try using a DataTrigger bound to the HasItems property.

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

In the style set the DataTemplate to the other template the the template will be replaced when the TreeView has a child items

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜