【发布时间】:2009-12-03 05:41:59
【问题描述】:
将参数分配给 WPF 窗口中的对象(像矩形一样简单的对象)并在鼠标单击时将它们传递给事件处理程序的正确方法是什么?
谢谢。
【问题讨论】:
标签: wpf parameters event-handling
将参数分配给 WPF 窗口中的对象(像矩形一样简单的对象)并在鼠标单击时将它们传递给事件处理程序的正确方法是什么?
谢谢。
【问题讨论】:
标签: wpf parameters event-handling
这是一种方法:
把它放在窗口中:
public static readonly RoutedUICommand clickCommand = new RoutedUICommand("TheCommand", "TheCommand", typeof(Window1));
void ClickRectangle(object sender, ExecutedRoutedEventArgs e)
{
if (e.Parameter == null)
return;
MessageBox.Show("parameter = " + e.Parameter.ToString());
}
然后在 XAML 中:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Grid>
<Rectangle Margin="50" Fill="Blue">
<Rectangle.CommandBindings>
<CommandBinding
Command="my:Window1.clickCommand" Executed="ClickRectangle" />
</Rectangle.CommandBindings>
<Rectangle.InputBindings>
<MouseBinding MouseAction="LeftClick"
Command="my:Window1.clickCommand" CommandParameter="123" />
</Rectangle.InputBindings>
</Rectangle>
</Grid>
</Window>
【讨论】: