在Silverlight中绑定到HierarchycalDataTemplate内的UserControl

在Silverlight中绑定到HierarchycalDataTemplate内的UserControl,silverlight,binding,datatemplate,Silverlight,Binding,Datatemplate,我在usercontrol中定制了TreeView。 在HierarchycalDataTemplate中,我有一个带方向的图像(示例为箭头) 当应用程序\用户控制流方向改变时,我需要翻转图像 因此,我尝试将Image.FlowDirection(在RightToLeft时自动翻转图像)绑定到UserControl.FlowDirection <Image FlowDirection="{Binding Path=FlowDirection,

我在usercontrol中定制了TreeView。 在HierarchycalDataTemplate中,我有一个带方向的图像(示例为箭头)

当应用程序\用户控制流方向改变时,我需要翻转图像

因此,我尝试将Image.FlowDirection(在RightToLeft时自动翻转图像)绑定到UserControl.FlowDirection

<Image FlowDirection="{Binding Path=FlowDirection, 
                               ElementName=MyUserControl}" ... />

但它不起作用。我猜是因为他在模板中找不到UserControl。 我在模板外尝试过这种绑定,效果很好

有解决办法吗? 如何从模板内部获取UserControl

--更新--

这个xaml中有两个绑定位置。第一个是按钮的样式,它正在工作,第二个是在TreeViewItem的模板中,但它不工作

<UserControl x:Class="MyTree"
...
    d:DesignHeight="218" d:DesignWidth="284" x:Name="MyLayerListControl">
<UserControl.Resources>
        <Style x:Key="CloseButtonStyle" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                ...
                            </VisualStateManager.VisualStateGroups>

                            <Border x:Name="CloseButtonBorder" BorderThickness="0" Margin="3" CornerRadius="0" Background="Gray" >

<!-- THIS BINDING IS WORKING -->
                                <Image Source="{StaticResource back}" Margin="2"
 FlowDirection="{Binding Path=FlowDirection, ElementName=MyLayerListControl}"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="Transparent">

<Button Style="{StaticResource CloseButtonStyle}" />    

<Grid>
     <Grid.ColumnDefinitions></Grid.ColumnDefinitions>
     <Grid.RowDefinitions>
          <RowDefinition Height="Auto"></RowDefinition>
          <RowDefinition Height="*"></RowDefinition>
     </Grid.RowDefinitions>


     <Grid.Resources>
         <sdk:HierarchicalDataTemplate x:Key="LayerListTemplate" ItemsSource="{Binding Path=Childrens}" >
             <Grid>
               <Border CornerRadius="2" BorderBrush="#FF6DBDD1" BorderThickness="1" Background="#FFBADDE9" Opacity="0.5"  /> 
               <StackPanel Orientation="Vertical">
                  <!-- The Item -->
                  <StackPanel Orientation="Horizontal" Margin="3">

<!-- THIS BINDING IS NOT WORKING -->
                     <Image x:Name="ArrowImage" Width="16" Height="16" Source="{StaticResource arrow}" 
FlowDirection="{Binding Path=FlowDirection, ElementName=MyLayerListControl}"/>

                  </StackPanel>

               </StackPanel>
            </Grid>
        </sdk:HierarchicalDataTemplate>
    </Grid.Resources>

    <sdk:TreeView ItemsSource="{Binding}" ItemTemplate="{StaticResource LayerListTemplate}" x:Name="MyTreeView" Grid.Row="1" />
</Grid>

</Grid>
</UserControl>

...
正如@Chris所说,在绑定路径前面添加“DataContext”,因为您要绑定到控件本身,所以如果FlowDirection是DataContext的一个属性,您需要拥有它

另外,确保绑定中有Mode=TwoWay

另外,您可以使用RelativeSource,这样就不必硬编码ElementName


签出:

尝试“{Binding Path=DataContext.FlowDirection,ElementName=MyUserControl}抱歉,这不起作用。您可以发布不起作用的内容的完整代码吗?请确保在xaml中也设置了x:Name=MyUserControl。该名称确实设置得很好,因为我在xaml中的其他地方使用了相同的绑定,并且它也在起作用。我在一个部门发布simplify xaml,问题是我使用的是silverlight 4-第三方限制。版本4中没有AncestorType。