Anatolii Gabuza
Anatolii Gabuza

Reputation: 6260

SL4. Binding by ElementName is not working within DataTemplate

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

Answers (2)

Justin XL
Justin XL

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

XAMeLi
XAMeLi

Reputation: 6289

[This should be a comment, but I exceeded the allowed character count]

I see two ways to solving this problem:

  1. inherit from 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.
  2. add a static class that will hold a dictionary to hold references to the elements; add an attached property (bool) with PropertyChangedCallback in the metadata - if new value is true: add the element (that the property is attached to) to the dictionary, if false: remove the element from the dictionary; the key for each element is it's name; the behavior will get two strings that are the names of the elements and will look for them in the dictionary.

Both ways are not so elegant, but that's Silverlight ... ;-)

Upvotes: 1

Related Questions