Vấn đề của tôi là tôi muốn xử lý một lệnh ở nhiều nơi. Ví dụ tôi có UserControl tùy chỉnh của tôi, nơi một Button được ràng buộc với một số lệnh. Tôi có một lệnh ràng buộc trong điều khiển đó nhưng tôi cũng có một lệnh ràng buộc trong một cửa sổ trong đó sử dụng điều khiển này.RoutedCommands Executed and PreviewExecuted events
Mục đích của tôi là thực hiện một số thao tác bên trong điều khiển trong khi không làm gián đoạn việc xử lý lệnh trong Cửa sổ.
Tôi đã thử nghiệm với các sự kiện đã thực thi và PreviewExecuted nhưng không có may mắn. Sau đó, tôi mô phỏng vấn đề trong một cửa sổ duy nhất (mã được đăng bên dưới).
<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;
}
}
}
Khi tôi nhấn nút duy nhất "CommandBindingWindow_PreviewExecuted" được in ra. Tại sao vậy? Tôi đã cố gắng đặt e.Handled thành false nhưng nó không tạo ra sự khác biệt. Bất cứ ai có thể giải thích hành vi này?
Có cách nào để buộc 'RoutedCommand' tiếp tục định tuyến không? –
@Matthew: Well .. Bạn luôn có thể cố gắng nâng cao sự kiện từ phương thức Thực thi. – VitalyB
liên kết tới WPF wiki đã chết. đã di chuyển ở đâu đó? – dtm