如果您处理PreviewMouseDown
的{{1}}事件且UserControl
内部为UserControl
,则不会触发内置本机检查事件,也不是点击事件(突出显示为蓝色)。如果您不处理预览事件,它可以正常工作。
我认为预览事件不会将事件标记为已处理,但即使使用
ToggleButton
之后将不会有点击或检查事件。
如何启用togglebutton的click and checked事件?
了解问题的代码:
WpfApplication1.cs
e.handled = false;
WpfApplication.Xaml
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void UserControl1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//working
MessageBox.Show("Preview down");
e.Handled = false; //this is what I tried
}
private void UserControl1_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//working
MessageBox.Show("preview up");
}
}
}
UserControl1.cs:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:UserControl1 PreviewMouseLeftButtonDown="UserControl1_PreviewMouseLeftButtonDown" PreviewMouseLeftButtonUp="UserControl1_PreviewMouseLeftButtonUp" ></local:UserControl1>
</Grid>
</Window>
UserControl1.xaml
namespace WpfApplication1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
//Not called
MessageBox.Show("test");
}
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
//not called
MessageBox.Show("test");
}
}
}
答案 0 :(得分:1)
当触发用户控件中的事件时,您需要为用户控件引发事件处理程序。这将上升链并处理两者的均衡。
点击ToggleButton
后,它会触发ToggleButton_Click
并触发事件UserControl1_PreviousMouseLeftButtonDown
。
UserControl1.ToggleButtonClick += new EventHandler(UserControl1_PreviewMouseLeftButtonDown);
private void UserControl1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Preview down");
// Handle the event
}
public event EventHandler ToggleButtonClick;
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("test");
// Handle the event
}