Parar navegação para controle de corrente em caso de erro de validação. Quão?
-
22-07-2019 - |
Pergunta
Eu tenho um problema com a minha aplicação WPF.
Eu tenho um datagrid (WPF Toolkit), eu tenho que gerenciar uma validação Row ... se o resultado de validação é falso E eu quero que a outra linha não é selecionável.
Por isso eu tenho que bloquear a seleção para linha atual que eu edição.
Como posso fazer? Alguma idéia?
Solução
Luke,
Você não é o primeiro a fazer esta pergunta. É uma grande desvantagem para a versão WPF atual onde não há evento como PreviewSelectionChangeEvent para Selector derivado controle. A única comunidade solução aceita para este problema é, naturalmente, uma solução hack. Aqui é a abordagem.
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];
}
}
A esperança que leva você a uma solução.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow