因此,我在 wpf 中有一個串列框,其中包含使用 WindowsFormsHost 作為模板(Windows 表單用戶控制元件),這樣做,我另外創建了自己的主機(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 元素(Listbox)中
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>
xml.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 主視窗中作業。
private void UserControl3Host_ConditionalClick(object sender, RoutedEventArgs e)
{
if (isCollapsed != null)
{
isCollapsed(this);
}
}
我嘗試在我的 xaml ConditionalClick="UserControl3Host_ConditionalClick"> 中使用,
然后在創建條件點擊的主機中使用 ConditionalClick = new RoutedEventHandler(userControl.label1_Click) 。我不知道,iw 是如何作業的。也許您知道,我如何在 Windows 表單控制元件中捕獲事件或引發事件
uj5u.com熱心網友回復:
處理您的UserControl3事件,并通過呼叫從 Windows 表單事件的事件處理程式WindowsFormsHost引發您的事件:ConditionalClickRaiseEvent(new RoutedEventArgs(ConditionalClickEvent))
public UserControl3Host()
{
Child = userControl;
userControl.label1.Click = (ss, ee) => RaiseEvent(new RoutedEventArgs(ConditionalClickEvent));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/486025.html
上一篇:使用Viewport3D在WPF中繪制2D彩色網格圖-提高性能的技巧?
下一篇:為什么PropertyChangedCallback方法不從DependencyPropertyOverrideMetadata執行?
