Reputation: 6260
There is an another headache with DataTemplate.
Description:
Using Silverlight 4, Mvvm, etc. (standart set of developer tools for Silverlight LOB applications).
List of entities is succesfully binded to DataGrid
. One property (nullable bool BoolValue) is responsible for entity behavior and presented in datagrid with picture, clicking on wich leads to changing visibility of some Controls within LayoutRoot element.
Problem:
The problem is that, unfortunately or fortunately, ElementName
binding within DataTemplate
does not see other elements except those, that are placed within this template.
Code Sample:
<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>
In the example above, FrameworkElementToHide and FrameworkElementToShow are always null.
There are a lot of pretty similar problems and solutions in internet, but I have not found any straightforward and elegant way of solving this problem.
Upvotes: 4
Views: 3236
Reputation: 39006
please take a look at my answer in this post.
ElementName binding is not working within DataGrid
. You need a proxy to work around this. However, ElementName binding does work for normal DataTemplates
, e.g. ItemTemplate
of a ListBox
, etc.
Upvotes: 6
Reputation: 6289
[This should be a comment, but I exceeded the allowed character count]
I see two ways to solving this problem:
ContentControl
; add IsShowing property (bool) that will toggle between two states; in the control template for the new control make the desired animations for showing and hiding the content. Both ways are not so elegant, but that's Silverlight ... ;-)
Upvotes: 1