Mój problem polega na tym, że chciałbym obsługiwać polecenia w wielu miejscach. Na przykład mam swój własny UserControl, w którym Button jest powiązany z jakimś poleceniem. Mam polecenie wiążące w tej kontroli, ale mam również powiązanie polecenia w oknie, które używa tego formantu.RoutedCommands Wykonywane i Podgląd Wykonywane zdarzenia
Moim celem jest wykonanie pewnej akcji wewnątrz kontroli bez przerywania obsługi polecenia w oknie.
Próbowałem eksperymentować z wydarzeniami wykonywanymi i podglądanymi, ale bez powodzenia. Następnie zasymulowałem problem w jednym oknie (kod zamieszczono poniżej).
<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;
}
}
}
Kiedy tylko nacisnąć przycisk "CommandBindingWindow_PreviewExecuted" jest drukowany. Dlaczego? Próbowałem ustawić e.Handled na false, ale to nie ma znaczenia. Czy ktoś może wyjaśnić to zachowanie?
Czy istnieje sposób zmuszenia 'RoutedCommand' do kontynuowania routingu? –
@Matthew: Cóż .. Zawsze możesz spróbować ponownie podnieść zdarzenie z metody Executed. – VitalyB
link do wiki WPF nie działa. czy to gdzieś się poruszyło? – dtm