在使用Silverlight 4,Mvvm等(Silverlight LOB应用程序的标准开发工具集)过程中,DataTemplate又成为了一个令人头疼的问题。
描述:实体列表已成功绑定到DataGrid
,其中一个属性(可空bool BoolValue)负责实体行为,并在datagrid中呈现为图片。点击图片会更改LayoutRoot元素中某些控件的可见性。
问题:不幸的是或幸运的是,DataTemplate
内的ElementName
绑定只能看到该模板内部包含的元素,无法看到其他元素。
代码示例:
<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始终为null。
在互联网上有很多类似的问题和解决方案,但我没有找到任何直接而优雅地解决这个问题的方法。
DataGrid
模板内部无法通过ElementName
进行绑定吗? - Anatolii GabuzaDataTemplate
内访问ViewModel的方法,但是在我的情况下使用StaticResource
似乎也可以正常工作。 - Anatolii Gabuza