0

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?

akjoshi
  • 15,374
  • 13
  • 103
  • 121
nakiya
  • 14,063
  • 21
  • 79
  • 118

2 Answers2

3

Try

Binding="{Binding Path=DataContext.IsDefined, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}}"
LPL
  • 16,827
  • 6
  • 51
  • 95
  • +1, this works. However now I find that when I change the value of `IsDefined` property to true, the `TreeView` does not get updated according to the template (property changed notification is fired.). Any idea as to why? Thanks. – nakiya Jul 18 '12 at 09:00
  • I think you should move your default ContentControl.Template into style too. This could be a matter of precedence. – LPL Jul 18 '12 at 09:11
  • +1 if I could again. Thanks. btw, what is the reason it does not work like the way I did and is it a bad idea to put default values in xaml if I intend to change some property's value later on? – nakiya Jul 18 '12 at 09:29
  • Read [Dependency Property Value Precedence](http://msdn.microsoft.com/en-us/library/ms743230.aspx) for the reason and no I think it is okay to do it in XAML. – LPL Jul 18 '12 at 09:33
1

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>     
akjoshi
  • 15,374
  • 13
  • 103
  • 121
  • The Setter for DefaultTemplate has to be in the style and your Binding in second example is the old one. – LPL Jul 18 '12 at 09:45