0

我有一种情况,我使用的HierarchicalDataTemplate大多数显示字段都绑定到此模板中表示的项目(如您所料),但我还需要一个Binding到其UserControl自身的数据上下文。我无法管理这最后一点。

所以我目前有:

<UserControl x:Class="MyProject.ProjectTreeView">

<UserControl.Resources>    
    <HierarchicalDataTemplate DataType="{x:Type StreetViewModel}" 
                              ItemsSource="{Binding Houses}">   

        <!-- This Binding works fine (binds to local:StreetViewModel.Street.StreetName) -->             
        <TextBlock Text="{Binding Street.StreetName}">
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Triggers>
                         <!-- THIS BINDING DOESN'T WORK (I want it to bind to local:ProjectTreeView.SelectedStreet) -->
                        <DataTrigger Binding="{Binding Path=SelectedStreet, RelativeSource={RelativeSource Self}, UpdateSourceTrigger=PropertyChanged}" Value="Main Street">
                            <Setter Property="FontWeight" Value="Bold" />
                        </DataTrigger>
                        <!-- This one works again (binds to local:StreetViewModel.Street.ConstructionWorkGoingOn) -->
                        <DataTrigger Binding="{Binding Street.ConstructionWorkGoingOn, UpdateSourceTrigger=PropertyChanged}"  Value="true">
                            <Setter Property="Foreground" Value="Red" />
                        </DataTrigger>
                   </Style.Triggers>
               </Style>
           </TextBlock.Style>
       </TextBlock>
   </HierarchicalDataTemplate>

所以有问题的事情是我想访问其中的数据,ProjectTreeView但无法在这段代码中访问它。我已经尝试了各种各样的东西,RelativeSource但这不起作用。如何才能做到这一点?

4

1 回答 1

2

尝试 :

   <DataTrigger Binding="{Binding Path=DataContext.SelectedStreet, RelativeSource={RelativeSource AncestorType=UserControl}}, UpdateSourceTrigger=PropertyChanged}" Value="Main Street">
         <Setter Property="FontWeight" Value="Bold" />
   </DataTrigger>
于 2013-11-05T16:00:28.673 回答