【问题标题】:Binding to the UserControl Inside HierarchicalDataTemplate in Silverlight在 Silverlight 中绑定到 HierarchicalDataTemplate 内的 UserControl
【发布时间】:2013-10-07 18:29:59
【问题描述】:

我在用户控件中自定义了 TreeView。 在 HierarchicalDataTemplate 中,我有一个带方向的图像(例如箭头)。

当 application\UserControl 流向改变时,我需要翻转图像。

所以我尝试将 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>

【问题讨论】:

  • 试试 '{Binding Path=DataContext.FlowDirection, ElementName=MyUserControl}
  • 对不起,这不起作用
  • 你能发布不工作的完整代码吗?
  • 确保 x:Name=MyUserControl 也在 xaml 中设置。
  • 该名称肯定设置得很好,因为我在 xaml 的其他地方使用相同的绑定并且它正在工作。我将在一秒钟内发布简化 xaml

标签: silverlight binding datatemplate


【解决方案1】:

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

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

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

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

【讨论】:

  • 问题是我正在使用 silverlight 4 - 3rd 方限制。并且版本 4 中没有 AncestorType。
猜你喜欢
  • 1970-01-01
  • 2010-12-08
  • 2014-12-22
  • 2011-07-29
  • 2011-08-31
  • 2011-07-13
  • 1970-01-01
  • 2010-12-04
  • 2011-07-16
相关资源
最近更新 更多