【发布时间】: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