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

发布于 2024-09-11 14:34:32 字数 359 浏览 8 评论 0原文

这是我当前的问题:

我有一个包含 4 列的数据网格:第一年按索引、第一年按百分比、第二年+按索引和第二年+按百分比。我希望我的数据网格使这些列与其对应列互斥。

例如,如果我按百分比输入第一年的数字,则不应允许我按索引输入第一年的任何内容,反之亦然。第二年+的专栏彼此也是如此。

如果不是锁定该列,而是删除其他列的值(即在上面的示例中,不是无法按索引编辑第一年,而是这样做,那么如果您这样做,它会删除该值,我会很高兴)从第一年开始按百分比计算)。

有什么想法吗?

编辑:这是我到目前为止所尝试的:当某些内容发生更改时,我尝试调整相邻列的“AllowEdit”字段(这没有执行任何操作),并且我尝试清除另一列中的值(也失败) 。

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. 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.

I would be happy enough if instead of locking the column it would erase the other column's value (i.e. 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).

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萝莉病 2024-09-18 14:34:32

如果您使用某种 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);
    }
}

编辑:您还可以添加一些布尔属性来绑定到每列的 IsReadOnly 属性并使用相同的属性将备用列的一设置为 true 的技术。

编辑:通过一些测试,我发现您必须将数据网格列的绑定 UpdateSourceTrigger 设置为 PropertyChanged。否则,在用户按下回车键或选择不同的行之前,上述设置器代码将不会运行。

PS:我使用的是 MVVM-Light,这是 RaisePropertyChanged 的​​来源。

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:

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.

Edit: With some testing I have found that you must set datagrid column's binding UpdateSourceTrigger to PropertyChanged. Otherwise the setter code described above will not run until the user either presses the enter key or selects a different row.

PS: I'm using MVVM-Light which is where the RaisePropertyChanged comes from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文