Question

I got the following code :

<HierarchicalDataTemplate x:Key="AssignedRate" ItemsSource="{Binding Children}" DataType="{x:Type local:UnitRateCatElement}">
    <ContentControl>
        <ContentControl.Template>
            <ControlTemplate>
                <StackPanel Tag="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}">
                    <TextBlock Text="{Binding Category.Description}" />
                    <StackPanel.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Add Unit Rate"
                                      Command="{Binding Path=PlacementTarget.Tag.AddUnitRateCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
                        </ContextMenu>
                    </StackPanel.ContextMenu>
                </StackPanel>
            </ControlTemplate>
        </ContentControl.Template>
        <ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsDefined, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}}" Value="True">
                        <Setter Property="Template">    
                            <Setter.Value>
                                <ControlTemplate>
                                    <TextBlock Text="Hello, it works!" />
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>
</HierarchicalDataTemplate>

The binding in the line : <DataTrigger Binding="{Binding Path=IsDefined, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}}" Value="True">

is incorrect (VS says so). How can I get this to work? The class local:UnitRateCatElement does have a IsDefined property. But I cannot get the binding right to point to that object. How can I get this binding right?

Was it helpful?

Solution

Try

Binding="{Binding Path=DataContext.IsDefined, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}}"

OTHER TIPS

I think you should be doing this -

<DataTrigger Binding="{Binding Path=DataContext.IsDefined, 
             RelativeSource={RelativeSource Mode=FindAncestor, 
             AncestorType={x:Type TreeViewItem}}}" Value="True">

as your RelativeSource binding points to the TreeViewItem, which doesn't have IsDefined property, it is present in the DataContext of the TreeViewItem.

Update:

For your second problem (trigger not working), this is happening because you are setting the Template explicitly i.e. Local Value which is having higher precedence then Triggers; this should work -

<ControlTemplate x:Key="DefaultTemplate">    
    <StackPanel Tag="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}">    
        <TextBlock Text="{Binding Category.Description}" />    
        <StackPanel.ContextMenu>    
            <ContextMenu>    
                <MenuItem Header="Add Unit Rate"    
                          Command="{Binding Path=PlacementTarget.Tag.AddUnitRateCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>    
            </ContextMenu>    
        </StackPanel.ContextMenu>    
    </StackPanel>    
</ControlTemplate> 

<ControlTemplate x:Key="DefinedTemplate">      
    <TextBlock Text="Hello, it works!" />      
</ControlTemplate>   

<HierarchicalDataTemplate x:Key="AssignedRate" ItemsSource="{Binding Children}" 
    DataType="{x:Type local:UnitRateCatElement}">      
    <ContentControl>     
        <ContentControl.Style>      
            <Style TargetType="{x:Type ContentControl}">     
                <Setter Property="Template" Value={StaticResource DefaultTemplate}>   
                <Style.Triggers>      
                    <DataTrigger Binding="{Binding Path=DataContext.IsDefined, RelativeSource=
                       {RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}}" 
                        Value="True">      
                       <Setter Property="Template" Value={StaticResource DefinedTemplate}>    
                    </DataTrigger>      
                </Style.Triggers>      
            </Style>      
        </ContentControl.Style>      
    </ContentControl>      
</HierarchicalDataTemplate>     
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top