绑定到Silverlight中的HierarchicalDataTemplate内的UserControl

时间:2013-10-01 14:27:34

标签: silverlight binding datatemplate

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

当应用程序\ UserControl流向更改时,我需要翻转图像。

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

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

但它不起作用。我猜是因为他无法从模板中找到UserControl。 我在模板外尝试了这种绑定 - 它运行正常。

任何解决方案? 如何从模板中获取UserControl?

- UPDATE -

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

1 个答案:

答案 0 :(得分:0)

正如@Chris所说,在绑定路径前添加“DataContext”,因为您绑定到Control本身,所以如果FlowDirection是DataContext的属性,则需要具有该属性。

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

作为替代方案,您可以使用RelativeSource,这样您就不必对ElementName进行硬编码。

退房:http://tonychampion.net/blog/index.php/2011/12/7th-day-of-silverlight-ancestor-relative-source-binding/