views:

42

answers:

2

Hi,

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

+2  A: 

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 "

}
saurabh
A: 

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

benPearce
Well, that won't work if i have more than two levels in the three wich need to have a different color.
Tokk
Yes, binding the trigger to a boolean property has limited options, but you could just as easily bind to a property on your datacontext to make this determination, such as an enumeration. Plus your question did state: If there are subitems or not
benPearce