Search code examples
c#wpfcomboboxcollectionviewsourcerowdetailstemplate

Combobox in RowDetailTemplate update all before selected colums


I Have a Combobox in a RowdetailsTemplate in a Datagrid. If i switch the columns then automatic change the value in the Datagridcolumn with the before selected Value. The Value in Datagrid column should only change if the value in the Combobox are changed

public class BMFill
{
    public BMFill()
    {
         colCBArt.Add(new CBArt { Name = "test" , Nr = 0 });
        colCBArt.Add(new CBArt { Name = "hallo", Nr = 1 });
        colCBArt.Add(new CBArt { Name = "welt", Nr = 2 });
        colCBArt.Add(new CBArt { Name = "blubb", Nr = 3 });
        colCBArt.Add(new CBArt { Name = "lalalala", Nr = 4 });

    }
    List<CBArt> colCBArt = new List<CBArt>();
    CollectionViewSource cvsCBArt = null;


    public ICollectionView ViewCBArt
    {
        get
        {
            if (cvsCBArt == null) cvsCBArt = new CollectionViewSource() { Source = colCBArt };
            return cvsCBArt.View;
        }
    }


    public class CBArt
    {
        public string Name { get; set; }
        public int Nr { get; set; }
    }
}

<Window.Resources>
    <local:BMFill x:Key="vm"/>
</Window.Resources>
<DataGrid x:Name="dg">
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <ComboBox Margin="10,10,10,10" Grid.Column="1" Grid.Row="1"
                                  SelectedValuePath="Nr"
                                  SelectedValue="{Binding NrDG,UpdateSourceTrigger=PropertyChanged}"
                                  DisplayMemberPath="Name" 
                                  ItemsSource="{Binding Source={StaticResource vm}, Path=ViewCBArt}"
                                  />
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
</DataGrid>

i hope can understand my problem and can me help =)


Solution

  • SelectedValuePath="Nr"
                                      SelectedValue="{Binding NrDG,UpdateSourceTrigger=PropertyChanged}"
                                      DisplayMemberPath="Name" 
                                      ItemsSource="{Binding Source={StaticResource vm}, Path=ViewCBArt}"
                                      IsSynchronizedWithCurrentItem="False"
    

    this Solution work by me