出现验证错误时停止导航至当前控件。如何?

发布于 2024-08-10 08:09:10 字数 139 浏览 6 评论 0原文

我的 WPF 应用程序有问题。

我有一个数据网格(Wpf Toolkit),我必须管理行验证...如果验证结果为 false,我会认为另一行不可选择。

因此,我必须阻止选择我编辑的当前行。

我该怎么办?有什么想法吗?

I have a problem with my WPF application.

I have a datagrid (Wpf Toolkit), I have to manage a Row validation...if validation result is false I would that the other row isn't selectable.

Therefore I have to block the selection to current row that I edit.

How can I do? Any ideas?

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

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

发布评论

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

评论(1

鹿! 2024-08-17 08:09:10

卢克,

你不是第一个问这个问题的人。它是当前 WPF 版本的一个主要缺点,其中没有选择器派生控件的 PreviewSelectionChangeEvent 等事件。对于这个问题,社区唯一接受的解决方案当然是 HACK 解决方案。这是方法。

public void OnSelectionChange(object sender, SelectionChangedEventArgs e)
{
    // Selector is based class for all selection enabled control
    // (not too sure if your datagrid
    // derives from the same class, you will need to check).
    var selector = e.OriginalSource as Selector;
    if (selector == null) return;

    // Get the old items and new items from the selection change
    // (note, that they are IList type).
    // Let's assume that your datagrid will only allow single cell selection only,
    // ie. newItems.Count == 1
    var newItems = e.AddedItems;
    var oldItems = e.RemovedItems;

    // May need to check if not null first.
    if (oldItems.Count == 1 && newItems.Count == 1)
    {
        // Checking logic for the first (and only) items.
        // Casting the item into our known type.
        var myObject = newItems[0] as myType;

        // Notice that I reversed the logic, this is because we are
        // only interested in when our logic fails and we need to revert
        // the selection to the old item,
        // otherwise the new item is selected by default
        if (!(myObject != null && SomeOtherCondition))
            selector.SelectedItem = oldItems[0];

    }

}

希望这能让您找到解决方案。

Luke,

You are not the first to ask this question. Its a major drawback to current WPF version where there is no event such as PreviewSelectionChangeEvent for Selector derived control. The only community accepted solution to this problem is of course a HACK solution. Here is the approach.

public void OnSelectionChange(object sender, SelectionChangedEventArgs e)
{
    // Selector is based class for all selection enabled control
    // (not too sure if your datagrid
    // derives from the same class, you will need to check).
    var selector = e.OriginalSource as Selector;
    if (selector == null) return;

    // Get the old items and new items from the selection change
    // (note, that they are IList type).
    // Let's assume that your datagrid will only allow single cell selection only,
    // ie. newItems.Count == 1
    var newItems = e.AddedItems;
    var oldItems = e.RemovedItems;

    // May need to check if not null first.
    if (oldItems.Count == 1 && newItems.Count == 1)
    {
        // Checking logic for the first (and only) items.
        // Casting the item into our known type.
        var myObject = newItems[0] as myType;

        // Notice that I reversed the logic, this is because we are
        // only interested in when our logic fails and we need to revert
        // the selection to the old item,
        // otherwise the new item is selected by default
        if (!(myObject != null && SomeOtherCondition))
            selector.SelectedItem = oldItems[0];

    }

}

Hope that leads you to a solution.

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