0

我得到以下代码:

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

行中的绑定: <DataTrigger Binding="{Binding Path=IsDefined, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}}" Value="True">

不正确(VS 这么说)。我怎样才能让它工作?该类local:UnitRateCatElement确实有一个IsDefined属性。但我无法获得指向该对象的绑定权。我怎样才能正确绑定?

4

2 回答 2

3

尝试

Binding="{Binding Path=DataContext.IsDefined, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}}"
于 2012-07-18T08:44:25.700 回答
1

我认为你应该这样做 -

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

由于您的RelativeSource绑定指向TreeViewItem没有IsDefined属性的 ,它存在DataContextTreeViewItem.

更新:

对于您的第二个问题(触发器不起作用),这是因为您正在显式设置模板,即具有更高优先级Triggers的本地值;这应该工作 -

<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>     
于 2012-07-18T08:48:46.297 回答