WPF如何将事件传递给兄弟元素
我已阅读帖子 Button.MouseDown 接下来我想再问 1 个问题。 我的XAML如下:
<Window.Resources>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="BorderThickness" Value="5" />
<Setter Property="Padding" Value="10" />
<Setter Property="Background" Value="PaleGreen" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel Tag="Panel" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown">
<ContentControl BorderBrush="Red" Tag="C1" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown">
<ContentControl BorderBrush="Green" Tag="C2" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown">
<StackPanel Tag="Panel2" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown">
<ContentControl Content="Click Me" BorderBrush="Blue" Tag="C3" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown"/>
<ContentControl Content="Click Me2" BorderBrush="Blue" Tag="C33" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown"/>
</StackPanel>
</ContentControl>
</ContentControl>
</StackPanel>
我的cs如下:
private void PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine(((FrameworkElement) sender).Tag + " Preview");
}
private void MouseDown(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine(((FrameworkElement) sender).Tag + " Bubble");
}
如何将鼠标向上/向下事件从C3传输到C33?基本上我希望来自一个兄弟姐妹的所有事件也能到达其他兄弟姐妹。
I have read the post Button.MouseDown
In continuation to that I want to ask 1 more question.
My XAML is as follows :
<Window.Resources>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="BorderThickness" Value="5" />
<Setter Property="Padding" Value="10" />
<Setter Property="Background" Value="PaleGreen" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel Tag="Panel" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown">
<ContentControl BorderBrush="Red" Tag="C1" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown">
<ContentControl BorderBrush="Green" Tag="C2" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown">
<StackPanel Tag="Panel2" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown">
<ContentControl Content="Click Me" BorderBrush="Blue" Tag="C3" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown"/>
<ContentControl Content="Click Me2" BorderBrush="Blue" Tag="C33" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown"/>
</StackPanel>
</ContentControl>
</ContentControl>
</StackPanel>
My cs is as follows :
private void PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine(((FrameworkElement) sender).Tag + " Preview");
}
private void MouseDown(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine(((FrameworkElement) sender).Tag + " Bubble");
}
How can I transfer mouse up/down events from C3 to C33? Basically I want all events from 1 sibling to reach other too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 C3 的 EventHandler 中,您可以调用 C33 的 RaiseEvent 方法:
顺便说一句,我注意到您正在使用 Tag 来命名元素。更合适的属性是 Name 属性。一般规则是,Tag 属性的使用仅适用于大约 0.001% 的 WPF 程序(十万分之一)。在所有常用的情况下,都有更好的方法来做同样的事情。
From C3's EventHandler you can call C33's RaiseEvent method:
As an aside, I notice that you are using Tag to name your elements. A much more appropriate property to use for this would be the Name property. A general rule is that the use of the Tag property is only appropriate in roughtly 0.001% of WPF programs (1 in 100,000). In all cases where it is commonly used there is a much better way to do the same thing.
我为这种情况编写了一个行为:
用法:
这将
MouseDown
和PreviewMouseDown
事件从A
重定向到B
。您需要
Install-Package System.Windows.Interactivity.WPF
到您的项目中,并需要 xmlns 声明:I wrote a behavior for this kind of scenario:
Usage:
This redirects
MouseDown
andPreviewMouseDown
events fromA
toB
.You need
Install-Package System.Windows.Interactivity.WPF
to your project, and xmlns declarations: