Search code examples
wpfdatagridcomboboxtextboxdatagridtemplatecolumn

WPF Change DataGridTextColumn when DataGridTemplateColumn->ComboBox changed, while ComboBox binding is different to TextColumn


I've been struggling with this problem for two weeks now. I've searched all over Google and Stackoverflow and I can't seem to find the correct answer.

Usually I don't like to ask questions unless it is absolutely necessary but I really can't figure this one out!

The problem is as follows.

I have an application with two ViewModels.

One contains a list of items that can be created at a separate page.

When the user goes back to the main application, I have a DataGrid and when I add a row to the DataGrid, a ComboBox is listed in a Column and this ComboBox contains items from the first ViewModel.

What I want to happen is that when the user selects an item from this ComboBox, I want the value from the ComboBox to come across to the current ViewModel of the DataGrid and change the value of another item in the DataGrid.

I have the following set up currently but I still can't get it to work.

I would preferably like to do this with straight XAML if possible.

Obviously the following XAML is wrong for the purpose.

<DataGrid Style="{StaticResource dataGridStyle}" ItemsSource="{Binding CurrentParser.InputVariables}" AutoGenerateColumns="False" Margin="6,6,35,6" Name="dgInputVarDefs">

<!--
<DataGrid.Triggers>
    <EventTrigger RoutedEvent="ComboBox.SelectionChanged">
    </EventTrigger>
</DataGrid.Triggers>
-->

<DataGrid.Columns>

    <DataGridTextColumn Header="#" Binding="{Binding Path=number}" />
    <DataGridTextColumn Header="Name" Binding="{Binding Path=name}" />

    <DataGridTemplateColumn x:Name="dgtcFormatter" Header="->Formatter">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ComboBox 
                    ItemsSource="{Binding Source={StaticResource FormatterViewModel}, 
                    Path=CurrentFormatter.formats}" 
                          x:Name="cbFormatter" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

    <DataGridTemplateColumn x:Name="dgtcFormat" Header="->Format">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox x:Name="dgtcFormatTextBox" Text="{Binding Path=format.format}"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

    <DataGridTextColumn Header="Separator" Binding="{Binding Path=separator}" />
    <DataGridTextColumn Header="Preview" Binding="{Binding Path=preview, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
</DataGrid.Columns>
</DataGrid>

Solution

  • I'm not sure how you have your viewmodels set up to your forms, but I tend to make sure I only have one viewmodel for each form.

    If your combobox uses the same viewmodel as the datagrid, then you could do use the following in XAML on your combo:

    <ComboBox ItemsSource="{Binding Path=DataContext.myCollection, RelativeSource={RelativeSource    
          Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
    

    This lets your combo use a different ItemsSource than the datagrid.