Frage

Ich habe ein Problem mit meiner WPF-Anwendung.

Ich habe ein Datagrid (WPF Toolkit), ich habe eine Reihe Validierung verwalten ..., wenn die Validierung Ergebnis ist falsch, ich würde, dass die andere Zeile nicht wählbar ist.

Deshalb muss ich in die aktuelle Zeile die Auswahl blockieren, die ich bearbeiten.

Wie kann ich tun? Irgendwelche Ideen?

War es hilfreich?

Lösung

Luke,

Sie sind nicht die erste Frage zu stellen. Es ist ein großer Nachteil auf aktuelle WPF-Version, wo es kein Ereignis wie PreviewSelectionChangeEvent für Selector ist abgeleitete Kontrolle. Die einzige Gemeinschaft für dieses Problem akzeptierte Lösung ist natürlich eine HACK Lösung. Hier ist der Ansatz.

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];

    }

}

Hoffnung, die Sie zu einer Lösung führt.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top