検証エラーで現在のコントロールへのナビゲーションを停止します。どうやって?

StackOverflow https://stackoverflow.com/questions/1649053

質問

WPFアプリケーションに問題があります。

データグリッド(Wpf Toolkit)があり、行検証を管理する必要があります...検証結果がfalseの場合、他の行は選択できません。

したがって、編集する現在の行の選択をブロックする必要があります。

どうすればいいですか?アイデアはありますか?

役に立ちましたか?

解決

ルーク、

この質問をするのはあなたが最初ではありません。 Selector派生コントロールのPreviewSelectionChangeEventなどのイベントがない現在のWPFバージョンの大きな欠点です。この問題に対するコミュニティが受け入れた唯一のソリューションは、もちろん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];

    }

}

解決策に導く希望。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top