1

My problem is basically described in the title of this topic.

I have a datagrid which is dynamically filled with various entries:

<DataGrid
    x:Name="searchResult"
    AutoGenerateColumns="True"
    IsSynchronizedWithCurrentItem="True"
    ItemsSource="{Binding Path=SearchResult}" />

Next to it is a bunch of TextBoxes, also dynamically generated:

<ItemsControl
    x:Name="searchForm"
    ItemTemplate="{StaticResource formInputTest}"
    ItemsSource="{Binding SearchForm, UpdateSourceTrigger=PropertyChanged}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

The template I use in the ItemsControl looks basically like this:

<TextBox
    Tag="{Binding Index}"
    Text="{Binding ElementName=searchResult, Path=SelectedValue.a, UpdateSourceTrigger=PropertyChanged}" />

Now lets asume my datagrid has 2 columns (a and b) and I have 2 TextBoxes with identical names (a and b). Now I want to select a datagridrow and like the two text boxes to be automatically filled with the values from the corresponding row.

Unfortunately, I don't know how to get this dynamically Binding. If I bind the TextBox's Text-property statically to searchResult->SelectedValue.a it works fine, but as you can see, only static. Thats what I've done in the third code-box above.

The ItemsControl, which contains the TextBoxes, has it's binding to "SearchForm". SearchForm has the property "Index", which is the name of the Column. So, the TextBox knows the name of the column it has to be binded to. To clarify this, I binded the TextBox.Tag to Index, which works fine.

Basically, I want my TextBox-Binding to look like this:

<TextBox
    Text="{Binding ElementName=searchResult, Path=SelectedValue.{Binding Index}, UpdateSourceTrigger=PropertyChanged}" />

But, as you might know, a binding inside a binding does not work.

Does anybody know how to bind these to dynamically generated objects together? Thanks for any hint!

UPDATE 2019/03/07

Thanks dymanoid! This has got me some progress but I'm still not at the finish line.

I created the view-model property and binded both the Datagrid-SelectedItem and the TextBox-Text property to it. Since the SelectedItem is a kind of a dictionary (BindablePublicDictionary) I use a converter (IMultiValueConverter) at the TextboxBinding, to convert the dictionary entry to both the index of the textbox and the text/value.

This is working fine, I select a datagrid-row and the 3 dynamically generated inputfields are beeing filled.

Unfortunatelly, if I edit one of these filled textboxes, the other 2 are emptied and my datagrid is unchanged.

I implemented OnPropertyChanged, my bindings are TwoWay and the ConvertBackFunction is called and returns the data in compliance with the targetType Type from the function itself.

Does anybody have experience with the bindings/converter between a datagrid/dictionary and textboxes, especially regarding the convertBack function?

  • What about using the [Bindig](https://learn.microsoft.com/de-de/dotnet/api/system.windows.data.binding?view=netframework-4.7.2) Class to create and attach a binding to the text box when you create it in your code? – devsmn Mar 01 '19 at 12:58
  • 1
    In your data grid, bind the currently selected item (row) to a view-model property (binding mode `TwoWay` or `OneWayToSource`). Then, bind your textboxes to that view-model property. This is a standard approach in WPF. – dymanoid Mar 01 '19 at 13:06
  • Yes as dymanoid suggested use a property on your view-model to mediate the transfer of data between the grid and textboxes. This is a simpler solution which doesn't need dynamic binding. – Mohammad Mar 01 '19 at 16:04

0 Answers0