0

我在 Datagrid 的 RowdetailsTemplate 中有一个组合框。如果我切换列,则使用之前选择的值自动更改 Datagridcolumn 中的值。只有在更改组合框中的值时,Datagrid 列中的值才会更改

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>

我希望能理解我的问题,我能帮忙吗=)

4

2 回答 2

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;
    }
于 2017-04-25T15:19:07.437 回答
0
SelectedValuePath="Nr"
                                  SelectedValue="{Binding NrDG,UpdateSourceTrigger=PropertyChanged}"
                                  DisplayMemberPath="Name" 
                                  ItemsSource="{Binding Source={StaticResource vm}, Path=ViewCBArt}"
                                  IsSynchronizedWithCurrentItem="False"

这个解决方案由我工作

于 2017-04-25T17:58:31.567 回答