简体   繁体   English

如何在WPF数据网格中创建互斥列?

[英]How do I make mutually exclusive columns in a WPF Data Grid?

Here's my current problem: 这是我目前的问题:

I have a data grid with 4 columns in it: Year One by Index, Year One by Percentage, Year Two+ by Index, and Year Two+ by Percentage. 我有一个包含4列的数据网格:按索引的第一年,按百分比的第一年,按索引的第二年以上,以及按百分比的第二年以上。 I want my data grid to make these columns mutually exclusive with its counterpart column. 我希望我的数据网格使这些列与其对应的列互斥。

So for example, if I type in a number for Year One by Percentage I shouldn't be allowed to enter anything for Year One by Index and vice versa. 因此,例如,如果我按百分比输入第一年的数字,则不允许我按索引输入第一年的任何内容,反之亦然。 Same goes with the Year Two+ columns with each other. Year ++列彼此相同。

I would be happy enough if instead of locking the column it would erase the other column's value (ie in the above example, instead of not being able to edit Year One by Index, make it so that if you did it it would erase the value from Year One by Percentage). 如果不锁定该列而不是擦除另一列的值(例如,在上面的示例中,而不是不能按索引编辑Year Year,则使其成为一个值,如果它做到了,则擦除该值),我会很高兴从第一年开始按百分比计算)。

Any ideas? 有任何想法吗?

EDIT: Here's what I've tried so far: I tried adjusting the "AllowEdit" field of the neighboring column when something gets change (this didn't do anything), and I tried clearing the value in the other column (also failed). 编辑:这是到目前为止我已经尝试过的操作:当发生更改时,我尝试调整相邻列的“ AllowEdit”字段(这没有做任何事情),并且尝试清除另一列中的值(同样失败) 。

If you are using some sort of MVVM pattern then in your viewmodel that represents the data for a row, you could do something like this: 如果您使用某种MVVM模式,则在表示行数据的视图模型中,您可以执行以下操作:

public const string YearOneByIndexPropertyName = "YearOneByIndex";
public int YearOneByIndex
{
    get
    {
        return _yearOneByIndex;
    }

    set
    {
        if (_yearOneByIndex  == value)
        {
            return;
        }

        _yearOneByIndex = value;
        _yearOneByPercentage = 0

        RaisePropertyChanged(YearOneByIndexPropertyName);
        RaisePropertyChanged(YearOneByPercentagePropertyName);
    }
}

public const string YearOneByPercentagePropertyName = "YearOneByPercentage";
public int YearOneByPercentage
{
    get
    {
        return _yearOneByPercentage;
    }

    set
    {
        if (_yearOneByPercentage == value)
        {
            return;
        }

        _yearOneByPercentage = value;
        _yearOneByIndex = 0;

        RaisePropertyChanged(YearOneByIndexPropertyName);
        RaisePropertyChanged(YearOneByPercentagePropertyName);
    }
}

Edit: You could also add some boolean properties to bind to the IsReadOnly properties of each column and use the same technique to set the alternate column's one to true. 编辑:您还可以添加一些布尔属性以绑定到每一列的IsReadOnly属性,并使用相同的技术将备用列的属性设置为true。

Edit: With some testing I have found that you must set datagrid column's binding UpdateSourceTrigger to PropertyChanged. 编辑:经过一些测试,我发现您必须将datagrid列的绑定UpdateSourceTrigger设置为PropertyChanged。 Otherwise the setter code described above will not run until the user either presses the enter key or selects a different row. 否则,直到用户按下Enter键或选择其他行,上述设置程序代码才会运行。

PS: I'm using MVVM-Light which is where the RaisePropertyChanged comes from. PS:我正在使用MVVM-Light,这是RaisePropertyChanged的来源。

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

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