Il mio problema è che mi piacerebbe gestire un comando in più posti. Ad esempio, ho il mio UserControl personalizzato in cui un pulsante è associato a un comando. Ho un binding di comando in quel controllo ma ho anche un binding di comando in una Window che usa questo controllo.RoutedCommands Executed and PreviewExecuted events
Il mio obiettivo è eseguire un'azione all'interno del controllo senza interrompere la gestione del comando nella finestra.
Ho provato a sperimentare eventi Eseguiti e Anteprima eseguiti ma senza fortuna. Quindi ho simulato il problema in una singola finestra (codice pubblicato di seguito).
<Window x:Class="CommandingEvents.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:CommandingEvents="clr-namespace:CommandingEvents"
Title="Window1" Height="300" Width="300">
<Window.CommandBindings>
<CommandBinding
Command="{x:Static CommandingEvents:Window1.Connect}"
Executed="CommandBindingWindow_Executed"
PreviewExecuted="CommandBindingWindow_PreviewExecuted"/>
</Window.CommandBindings>
<Grid>
<Grid.CommandBindings>
<CommandBinding
Command="{x:Static CommandingEvents:Window1.Connect}"
Executed="CommandBindingGrid_Executed"
PreviewExecuted="CommandBindingGrid_PreviewExecuted" />
</Grid.CommandBindings>
<Button Command="{x:Static CommandingEvents:Window1.Connect}"
CommandTarget="{Binding RelativeSource={RelativeSource Self}}"
Content="Test" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
namespace CommandingEvents
{
public partial class Window1
{
public static readonly RoutedUICommand Connect = new
RoutedUICommand("Connect", "Connect", typeof(Window1));
public Window1()
{
InitializeComponent();
}
private void CommandBindingWindow_Executed(object sender, ExecutedRoutedEventArgs e)
{
Console.WriteLine("CommandBindingWindow_Executed");
e.Handled = false;
}
private void CommandBindingGrid_Executed(object sender, ExecutedRoutedEventArgs e)
{
Console.WriteLine("CommandBindingGrid_Executed");
e.Handled = false;
}
private void CommandBindingWindow_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
{
Console.WriteLine("CommandBindingWindow_PreviewExecuted");
e.Handled = false;
}
private void CommandBindingGrid_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
{
Console.WriteLine("CommandBindingGrid_PreviewExecuted");
e.Handled = false;
}
}
}
Quando mi ha colpito il pulsante unico "CommandBindingWindow_PreviewExecuted" viene stampato. Perché? Ho provato a impostare e.Handled su false ma non fa differenza. Qualcuno può spiegare questo comportamento?
C'è un modo per forzare il 'RoutedCommand' per continuare il routing? –
@Matthew: Beh, puoi sempre provare a controrilanciare l'evento dal metodo Executed. – VitalyB
il collegamento al wiki WPF è morto. è spostato da qualche parte? – dtm