如何从嵌套控件事件引发宿主事件
How to raise a host event from a nested control event
所以,我在 wpf 中有一个列表框,其中包含使用 WindowsFormsHost 作为模板(windows 表单用户控件),o 这样做,我另外创建了自己的主机(UserControl3Host),它存储依赖属性和 windows 表单用户控件事件 属性,参见代码
主机
public class UserControl3Host : WindowsFormsHost
{
private readonly UserControl3 userControl = new UserControl3() { Title = string.Empty,
Date = string.Empty };
public delegate void expandedDel2(object sender);
public UserControl3Host()
{
Child = userControl;
ConditionalClick += new RoutedEventHandler(userControl.label1_Click);
}
/// <summary>
/// Заголовок контрола.
/// </summary>
/// <summary>
/// Заголовок контрола.
/// </summary>
public event RoutedEventHandler ConditionalClick
{
add
{
AddHandler(ConditionalClickEvent, value);
}
remove
{
RemoveHandler(ConditionalClickEvent, value);
}
}
public string Title
{
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
public string Date
{
get => (string)GetValue(DateProperty);
set => SetValue(DateProperty, value);
}
public static readonly RoutedEvent ConditionalClickEvent =
EventManager.RegisterRoutedEvent(
name: "ConditionalClick",
routingStrategy: RoutingStrategy.Tunnel,
handlerType: typeof(RoutedEventHandler),
ownerType: typeof(UserControl3Host));
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register(nameof(Title), typeof(string), typeof(UserControl3Host),
new PropertyMetadata(string.Empty, OnTitleChanged, OnCoerceTitle));
public static readonly DependencyProperty DateProperty =
DependencyProperty.Register(nameof(Date), typeof(string), typeof(UserControl3Host), new
PropertyMetadata(string.Empty, OnDateChanged, OnCoerceTitle));
private static void OnDateChanged(DependencyObject d, DependencyPropertyChangedEventArgs
e)
{
UserControl3Host host = (UserControl3Host)d;
host.userControl.Date = e.NewValue.ToString();
}
private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs
e)
{
UserControl3Host host = (UserControl3Host)d;
host.userControl.Title = e.NewValue.ToString();
}
private static object OnCoerceTitle(DependencyObject d, object baseValue)
{
return baseValue ?? string.Empty;
}
}
}
主机本身位于 wpf 元素(列表框)中
Xaml
<UserControl x:Class="WindowsFormsApp12.UserControl4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WindowsFormsApp12"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<ListBox.ItemTemplate>
<DataTemplate DataType="local:Line">
<Border Style="{StaticResource MessageBorder}" HorizontalAlignment="Stretch">
<StackPanel Orientation="Vertical" MouseDown="StackPanel_MouseDown">
<local:UserControl3Host Title="{Binding Name}" Date="{Binding Date}"
ConditionalClick="UserControl3Host_ConditionalClick">
</local:UserControl3Host>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Border>
</UserControl>
xaml.cs
public partial class UserControl4 : System.Windows.Controls.UserControl
{
public UserControl4()
{
InitializeComponent();
}
public delegate void expandedDel(object sender);
public event expandedDel isCollapsed;
private void UserControl3Host_ConditionalClick(object sender, RoutedEventArgs e)
{
if (isCollapsed != null)
{
isCollapsed(this);
}
}
}
代码Windows形成元素
namespace WindowsFormsApp12
{
public partial class UserControl3 : UserControl
{
public readonly UserControl5 userControl2 = new UserControl5() { };
#region Propertyies
public UserControl3()
{
InitializeComponent();
}
private string _title;
private string _date;
private Image _image;
private Color _iconBack;
[Category("Custom Props")]
public string Title
{
get {return _title; }
set {_title = value; label1.Text = value; }
}
[Category("Custom Props")]
public string Date
{
get { return _date; }
set { _date = value; label2.Text = value; }
}
[Category("Custom Props")]
public Image Image
{
get { return _image; }
set { _image = value;pictureBox1.Image = value; }
}
[Category("Custom Props")]
public Color IconBackGround
{
get { return _iconBack; }
set { _iconBack = value; panel1.BackColor =value ; }
}
#endregion
public delegate void CallFormMethod(object obj,string text); //делегат
public event CallFormMethod onButtonClick;
private void UserControl3_MouseEnter(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(30, 35, 40);
}
private void UserControl3_MouseLeave_1(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(50, 50, 50);
}
public void label1_Click(object sender, EventArgs e)
{
onButtonClick(this,label1.Text);
}
public void label2_Click(object sender, EventArgs e)
{
onButtonClick(this, label1.Text);
}
public void pictureBox1_Click(object sender, EventArgs e)
{
onButtonClick(this, label1.Text);
}
public void panel2_Click(object sender, EventArgs e)
{
onButtonClick(this, label1.Text);
}
private void UserControl3_Click(object sender, EventArgs e)
{
onButtonClick(this, label1.Text);
}
}
现在谈谈问题本身!启动程序并单击列表框中的用户控件时。事件发生在标签点击、面板点击...(Windows 形成事件)。我需要在 wpf main window 中使用处理程序处理该事件。
private void UserControl3Host_ConditionalClick(object sender, RoutedEventArgs e)
{
if (isCollapsed != null)
{
isCollapsed(this);
}
}
我尝试在我的 xaml ConditionalClick="UserControl3Host_ConditionalClick"> 之后使用
ConditionalClick += new RoutedEventHandler(userControl.label1_Click) 在我创建条件点击的主机中。我不知道,我是如何工作的。也许你知道,我如何在我的 windows 表单控件
中捕获事件或引发事件
在您的 WindowsFormsHost
中处理 UserControl3
的事件,并通过调用 RaiseEvent(new RoutedEventArgs(ConditionalClickEvent))
从 Windows 表单事件的事件处理程序中引发您的 ConditionalClick
事件:
public UserControl3Host()
{
Child = userControl;
userControl.label1.Click += (ss, ee) => RaiseEvent(new RoutedEventArgs(ConditionalClickEvent));
}
所以,我在 wpf 中有一个列表框,其中包含使用 WindowsFormsHost 作为模板(windows 表单用户控件),o 这样做,我另外创建了自己的主机(UserControl3Host),它存储依赖属性和 windows 表单用户控件事件 属性,参见代码
主机
public class UserControl3Host : WindowsFormsHost
{
private readonly UserControl3 userControl = new UserControl3() { Title = string.Empty,
Date = string.Empty };
public delegate void expandedDel2(object sender);
public UserControl3Host()
{
Child = userControl;
ConditionalClick += new RoutedEventHandler(userControl.label1_Click);
}
/// <summary>
/// Заголовок контрола.
/// </summary>
/// <summary>
/// Заголовок контрола.
/// </summary>
public event RoutedEventHandler ConditionalClick
{
add
{
AddHandler(ConditionalClickEvent, value);
}
remove
{
RemoveHandler(ConditionalClickEvent, value);
}
}
public string Title
{
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
public string Date
{
get => (string)GetValue(DateProperty);
set => SetValue(DateProperty, value);
}
public static readonly RoutedEvent ConditionalClickEvent =
EventManager.RegisterRoutedEvent(
name: "ConditionalClick",
routingStrategy: RoutingStrategy.Tunnel,
handlerType: typeof(RoutedEventHandler),
ownerType: typeof(UserControl3Host));
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register(nameof(Title), typeof(string), typeof(UserControl3Host),
new PropertyMetadata(string.Empty, OnTitleChanged, OnCoerceTitle));
public static readonly DependencyProperty DateProperty =
DependencyProperty.Register(nameof(Date), typeof(string), typeof(UserControl3Host), new
PropertyMetadata(string.Empty, OnDateChanged, OnCoerceTitle));
private static void OnDateChanged(DependencyObject d, DependencyPropertyChangedEventArgs
e)
{
UserControl3Host host = (UserControl3Host)d;
host.userControl.Date = e.NewValue.ToString();
}
private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs
e)
{
UserControl3Host host = (UserControl3Host)d;
host.userControl.Title = e.NewValue.ToString();
}
private static object OnCoerceTitle(DependencyObject d, object baseValue)
{
return baseValue ?? string.Empty;
}
}
}
主机本身位于 wpf 元素(列表框)中
Xaml
<UserControl x:Class="WindowsFormsApp12.UserControl4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WindowsFormsApp12"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<ListBox.ItemTemplate>
<DataTemplate DataType="local:Line">
<Border Style="{StaticResource MessageBorder}" HorizontalAlignment="Stretch">
<StackPanel Orientation="Vertical" MouseDown="StackPanel_MouseDown">
<local:UserControl3Host Title="{Binding Name}" Date="{Binding Date}"
ConditionalClick="UserControl3Host_ConditionalClick">
</local:UserControl3Host>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Border>
</UserControl>
xaml.cs
public partial class UserControl4 : System.Windows.Controls.UserControl
{
public UserControl4()
{
InitializeComponent();
}
public delegate void expandedDel(object sender);
public event expandedDel isCollapsed;
private void UserControl3Host_ConditionalClick(object sender, RoutedEventArgs e)
{
if (isCollapsed != null)
{
isCollapsed(this);
}
}
}
代码Windows形成元素
namespace WindowsFormsApp12
{
public partial class UserControl3 : UserControl
{
public readonly UserControl5 userControl2 = new UserControl5() { };
#region Propertyies
public UserControl3()
{
InitializeComponent();
}
private string _title;
private string _date;
private Image _image;
private Color _iconBack;
[Category("Custom Props")]
public string Title
{
get {return _title; }
set {_title = value; label1.Text = value; }
}
[Category("Custom Props")]
public string Date
{
get { return _date; }
set { _date = value; label2.Text = value; }
}
[Category("Custom Props")]
public Image Image
{
get { return _image; }
set { _image = value;pictureBox1.Image = value; }
}
[Category("Custom Props")]
public Color IconBackGround
{
get { return _iconBack; }
set { _iconBack = value; panel1.BackColor =value ; }
}
#endregion
public delegate void CallFormMethod(object obj,string text); //делегат
public event CallFormMethod onButtonClick;
private void UserControl3_MouseEnter(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(30, 35, 40);
}
private void UserControl3_MouseLeave_1(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(50, 50, 50);
}
public void label1_Click(object sender, EventArgs e)
{
onButtonClick(this,label1.Text);
}
public void label2_Click(object sender, EventArgs e)
{
onButtonClick(this, label1.Text);
}
public void pictureBox1_Click(object sender, EventArgs e)
{
onButtonClick(this, label1.Text);
}
public void panel2_Click(object sender, EventArgs e)
{
onButtonClick(this, label1.Text);
}
private void UserControl3_Click(object sender, EventArgs e)
{
onButtonClick(this, label1.Text);
}
}
现在谈谈问题本身!启动程序并单击列表框中的用户控件时。事件发生在标签点击、面板点击...(Windows 形成事件)。我需要在 wpf main window 中使用处理程序处理该事件。
private void UserControl3Host_ConditionalClick(object sender, RoutedEventArgs e)
{
if (isCollapsed != null)
{
isCollapsed(this);
}
}
我尝试在我的 xaml ConditionalClick="UserControl3Host_ConditionalClick"> 之后使用
ConditionalClick += new RoutedEventHandler(userControl.label1_Click) 在我创建条件点击的主机中。我不知道,我是如何工作的。也许你知道,我如何在我的 windows 表单控件
在您的 WindowsFormsHost
中处理 UserControl3
的事件,并通过调用 RaiseEvent(new RoutedEventArgs(ConditionalClickEvent))
从 Windows 表单事件的事件处理程序中引发您的 ConditionalClick
事件:
public UserControl3Host()
{
Child = userControl;
userControl.label1.Click += (ss, ee) => RaiseEvent(new RoutedEventArgs(ConditionalClickEvent));
}