1

Does anyone know how to dynamically set the ElementName for textblock.text binding?

I have two Datagrids that have the same information but the second DataGrid is just a filter of the same datasource, but what I want is to bind text of a textblock to the selected item depending if the item was clicked in the main datagrid or the secondary datagrid.

I have the below code to bind the textblock to one datagrid but I would also like the same to happen if the user clicked an item in the secondDataGrid.

Is this possible?

<TextBlock Margin="29,0" Text="{Binding SelectedItem.Name, ElementName=MainDataGrid}"
ExcelNoobie25
  • 39
  • 1
  • 7
  • 1
    Do they share same `ItemsSource`? If yes will be ok if you selected item in one `DataGrid` will be automatically selected in the other `DataGrid` (synchronized selected item) – dkozl Mar 15 '16 at 18:47
  • They do share the same ItemSource, but I'm not familiar with synchronized selected item.. can you explain further? – ExcelNoobie25 Mar 15 '16 at 19:01

1 Answers1

0

It is possible, though I don't think this is the proper solution.

You can handle one of the DataGrids' events in the code-behind, where in the handler you can write the following code:

BindingOperations.SetBinding(textBlock, TextBlock.TextProperty,
                             new Binding("SelectedItem.Name")
                             {
                                 ElementName = "DataGrid1"
                             });

Basically you reset the Binding on the TextBlock's Text property with this code, where:

  • textBlock is the name of your TextBlock;

  • with TextBlock.TextProperty you define that you want to work with the Text property on the TextBlock;

  • The third paramter is the new Binding itself. The constructor takes the Path of the Binding and then in the "body" I set the ElementName.

If DataGrid1 fires the event you set the ElementName to that DataGrid's name, if DataGrid2 fires the event then you set the ElementName to the second DataGrid's name.

SelectionChanged can be a good event to handle on both DataGrid, but if you want the TextBlock to update when you selected and element in the first then select another one in the second and then click back to the first element to update then you need to handle the GotFocus event as well. Play a little bit with it and you will see what I mean.

My working example:

private void SetBindingOnTextBlock(string elementName)
{
    BindingOperations.SetBinding(textBlock, TextBlock.TextProperty, new Binding("SelectedItem.Name")
    {
        ElementName = elementName
    });
}

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    SetBindingOnTextBlock("DataGrid1");
}

private void DataGrid_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
    SetBindingOnTextBlock("DataGrid2");
}

private void DataGrid1_GotFocus(object sender, RoutedEventArgs e)
{
    SetBindingOnTextBlock("DataGrid1");
}

private void DataGrid2_GotFocus(object sender, RoutedEventArgs e)
{
    SetBindingOnTextBlock("DataGrid2");
}

UPDATE 1:

Set

IsSynchronizedWithCurrentItem="True"

on the DataGrids and it may solve your problem if ItemsSources are the same. (Not sure if this is what @dkozl meant) Originally I assumed that they are different.

Akos
  • 91
  • 6
  • This looks like it will work, but I was hoping to keep it all in xaml if I could. If nothing else turns up I will try this method. Thank you. – ExcelNoobie25 Mar 15 '16 at 19:02
  • that did it! figured it had to be something simple that I was missing. I'm new to wpf so its a learning process.. do you know of any drawbacks to using IsSynchronizedWithCurrentItem="True"? – ExcelNoobie25 Mar 15 '16 at 19:29
  • @ExcelNoobie25 Not that I know of at the moment. A little bit of reading to be clear on what is happening here: [link](https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.issynchronizedwithcurrentitem%28v=vs.110%29.aspx) The example is a simple version of your scenario. [link](http://stackoverflow.com/questions/1822421/issynchronizedwithcurrentitem-attribute-and-current-item-updates) And another one in the answer. Good luck with WPF! ;) – Akos Mar 15 '16 at 19:42