简体   繁体   English

更改WPF DataGrid中其他ComboBox的值

[英]Change value of other ComboBox within a WPF DataGrid

Here is my problem. 这是我的问题。 I am pulling in data from a webserver (with a web request) into a ObservableCollection 我正在将数据从Web服务器(带有Web请求)拉入ObservableCollection

ObservableCollection<TestData> fillData = new ObservableCollection<TestData>();

From there I fill a data grid with the information. 从那里,我用信息填充数据网格。 One of those fields within that data grid is a combobox. 该数据网格内的那些字段之一是组合框。 Depending on the amount of data that get's pulled in you can have several lines with data. 根据要提取的数据量,您可以有几行数据。

 <DataGridTemplateColumn Header="Role">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Role}">
                            </TextBlock>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{StaticResource RoleList}" SelectedItem="{Binding Role}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>

The combobox can have 3 values. 组合框可以有3个值。 Primary, Secondary and Loaner. 小学,中学和贷款人。 When you change the value inside of the combobox then the ObservableCollection is updated like it should. 当您更改组合框内部的值时,ObservableCollection会像应有的那样进行更新。 The problem lies that you can only have one Primary selected, so when you change the value of one of the combo-boxes to primary then I have 2 or more primaries in my ObservableCollection. 问题在于您只能选择一个Primary,因此当您将组合框之一的值更改为Primary时,我的ObservableCollection中就有2个或更多Primary。

I am looking for a way that when you change a value of one of the combo-boxes to primary the other values that are also primary in the datagrid (combo-boxes) will be changed to secondary. 我正在寻找一种方法,当您将组合框之一的值更改为主要值时,在数据网格(组合框)中也是主要值的其他值将更改为次要值。 So that you only have one primary at the time. 这样您一次只有一个主数据库。 I am a bit lost, hope that anybody can give me some tips. 我有点迷茫,希望任何人都可以给我一些提示。 :) :)

You should look into eventing. 您应该调查事件。

When the Role property is changed, or when any of the Combobox SelectedValue's change, you need to loop through the list and make any changes needed. 当Role属性更改时,或任何Combobox SelectedValue更改时,您都需要遍历列表并进行所需的任何更改。

You could add an eventhandler for the combobox SelectionChanged event and do the check then Or perhaps the ObservableCollection.CollectionChanged for the datasource of the grid. 您可以为组合框SelectionChanged事件添加一个事件处理程序,然后进行检查,或者对网格的数据源执行ObservableCollection.CollectionChanged检查。

Here's an example of how you might do that. 这是您可能如何执行此操作的示例。

In any given collection of row items (whatever you call the class in your ObservableCollection), all the items in the collection have to have references to the same RoleCoordinator instance. 在任何行项目的给定集合中(无论您在ObservableCollection中调用什么类),集合中的所有项目都必须具有对同一RoleCoordinator实例的引用。 There's more than one way to implement that; 实现这一目标的方法不止一种。 you could make RoleCoordinator a public property of the row item, handle CollectionChanged on the ObservableCollection, and give each item a role coordinator as it's added to the collection. 您可以将RoleCoordinator设为行项目的公共属性,处理ObservableCollection上的CollectionChanged,并在将每个项目添加到集合时为其赋予角色协调器。 Here, I just passed it into the constructor because I'm initializing the collection statically in my window constructor for testing. 在这里,我只是将其传递给构造函数,因为我正在窗口构造函数中静态初始化集合以进行测试。

public class RoleCoordinator
{
    public event EventHandler<RoleChangedEventArgs> RoleChanged;

    public void RaiseRoleChanged(object sender, Role role)
    {
        RoleChanged?.Invoke(sender, new RoleChangedEventArgs(role));
    }
}

public class RoleChangedEventArgs : EventArgs
{
    public RoleChangedEventArgs(Role role)
    {
        Role = role;
    }

    public Role Role { get; set; }
}

Row item viewmodel 行项目视图模型

public class RowItemViewModel : ViewModelBase
{
    public RowItemViewModel(RoleCoordinator coordinator)
    {
        _roleCoordinator = coordinator;

        _roleCoordinator.RoleChanged += _roleCoordinator_RoleChanged;
    }

    #region Role Coordination
    private void _roleCoordinator_RoleChanged(object sender, RoleChangedEventArgs e)
    {
        if (sender != this && e.Role == Role.Primary && this.Role == Role.Primary)
        {
            Role = Role.Secondary;
        }
    }

    private RoleCoordinator _roleCoordinator;
    #endregion Role Coordination

    #region Role Property
    private Role _role = default(Role);
    public Role Role
    {
        get { return _role; }
        set
        {
            if (value != _role)
            {
                _role = value;

                _roleCoordinator.RaiseRoleChanged(this, this.Role);

                OnPropertyChanged();
            }
        }
    }
    #endregion Role Property

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM