如何为项而不是TreeViewItem获取此绑定?

时间:2012-07-18 08:40:24

标签: c# .net wpf treeviewitem hierarchicaldatatemplate

我收到了以下代码:

<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属性。但我无法获得指向该对象的约束权。我怎样才能正确绑定这个绑定?

2 个答案:

答案 0 :(得分:3)

尝试

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

答案 1 :(得分:1)

我认为你应该这样做 -

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

由于您的RelativeSource绑定指向TreeViewItem,但IsDefined没有DataContext属性,它会出现在TreeViewItem的{​​{1}}中。

更新

对于您的第二个问题(触发不起作用),这种情况正在发生,因为您正在明确设置模板,即具有更高precedence然后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>