RowDetailTemplate中的Combobox在选定的列之前更新所有内容

时间:2017-04-25 14:54:10

标签: c# wpf combobox collectionviewsource rowdetailstemplate

我在Datagrid的RowdetailsTemplate中有一个Combobox。如果我切换列,则自动使用之前选择的值更改Datagrid列中的值。 Datagrid列中的值只应在Combobox中的值更改时更改

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>

我希望能理解我的问题并且可以帮助我=)

2 个答案:

答案 0 :(得分:0)

您可以尝试为DropDownOpened和DropDownClosed事件添加事件处理程序,在打开下拉列表时引发标志,并在更改Datagrid列中的值时检查是否未引发此标志。

XAML:

        <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}"
                              DropDownOpened="OnDropDownOpened" DropDownClosed="OnDropDownClosed"
                              />

C#:

    private bool _comboxBoxIsOpened = false;
    private void OnDropDownOpened(object sender, EventArgs e)
    {
        _comboxBoxIsOpened = true;
    }

    private void OnDropDownClosed(object sender, EventArgs e)
    {
        _comboxBoxIsOpened = false;
    }

答案 1 :(得分:0)

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

此解决方案由我工作