DataTemplate还有一个令人头痛的地方。
描述:使用Silverlight 4、Mvvm等的( Silverlight LOB应用程序开发工具的标准集)。
实体列表成功地绑定到DataGrid。有一个属性(可空的bool BoolValue)负责实体行为,并以数据化的图片显示,单击wich会改变LayoutRoot元素中某些控件的可见性。
问题是,不幸的是,或者幸运的是,ElementName DataTemplate 中的绑定没有看到其他元素,只有这些元素放在这个模板中。
代码示例:
<navigation:DataGridTemplateColumn Width="40"
CanUserReorder="True"
CanUserSort="False">
<navigation:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Border Background="GhostWhite">
<Grid>
<Image x:Name="ImageWithTrigger"
Grid.Column="1"
Margin="10,4,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Cursor="Hand"
Source="images/someImage.png"
Stretch="None"
Visibility={Binding BoolValue, Converter={StaticResource boolToVisibilityConverter} }>
<i.Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<AttachedBehaviors:TrickyBehavior FrameworkElementToHide="{Binding ElementName=FirstControlOutside}"
FrameworkElementToShow="{Binding ElementName=SecoundControlOutside}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Grid>
</Border>
</DataTemplate>
</navigation:DataGridTemplateColumn.CellTemplate>
</navigation:DataGridTemplateColumn>在上面的例子中,FrameworkElementToHide和FrameworkElementToShow总是空的。
在互联网上有很多类似的问题和解决方案,但我没有找到任何简单而优雅的方法来解决这个问题。
发布于 2011-11-08 01:37:26
请看一下我在这个post中的答案。
ElementName绑定在DataGrid中无法工作。你需要一个代理来解决这个问题。然而,ElementName绑定确实适用于正常的DataTemplates,例如ListBox的ItemTemplate等等。
发布于 2011-11-07 21:33:56
这应该是注释,但我超过了允许的字符计数。
我看到了解决这个问题的两种方法:
ContentControl继承;添加将在两种状态之间切换的IsShowing属性(bool);在新控件的控件模板中,为显示和隐藏内容制作所需的动画。中查找它们。
两种方式都不那么优雅,但那是银光. ;-)
https://stackoverflow.com/questions/8042442
复制相似问题