简体   繁体   English

如何从嵌套控件事件引发宿主事件

[英]How to raise a host event from a nested control event

So, I have a listbox in wpf that contains as a template (windows forms user control) using WindowsFormsHost,o do this, I additionally created my own host (UserControl3Host), which stores the dependency properties and the windows forms user control event property,see code因此,我在 wpf 中有一个列表框,其中包含使用 WindowsFormsHost 作为模板(Windows forms 用户控件),为此,我另外创建了自己的主机(UserControl3Host),它存储依赖属性和 windows forms 用户控件事件属性,见代码

Host主持人

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;
    }
   
  }
 } 

The host itself is located in the wpf element(Listbox)主机本身位于 wpf 元素(列表框)

Xaml 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 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);
        }
    }
}

Code Windows forms element代码 Windows forms 元

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);
    }
 }

Now about the problem itself.现在谈谈问题本身。 When launching the program and clicking on the user control in the listbox, Events occur in label click.当启动程序并单击列表框中的用户控件时,事件发生在 label 单击。 panel click...(Windows forms events).面板点击...(Windows forms 事件)。 I need that event work in wpf main window with handler.我需要该事件在 wpf main window 中与处理程序一起工作。

 private void UserControl3Host_ConditionalClick(object sender, RoutedEventArgs e)
        {
            if (isCollapsed != null)
            {
                isCollapsed(this);
            }
        }  

i try to use in my xaml ConditionalClick="UserControl3Host_ConditionalClick"> and after what我尝试在我的 xaml ConditionalClick="UserControl3Host_ConditionalClick"> 中使用,之后
ConditionalClick += new RoutedEventHandler(userControl.label1_Click) in my host where conditional click was created. ConditionalClick += new RoutedEventHandler(userControl.label1_Click) 在我创建条件点击的主机中。 I dont know, how iw works.我不知道,我是如何工作的。 Maybe you know,how i can catch event or raise event in my windows forms control也许你知道,我如何在我的 windows forms 控件中捕获事件或引发事件

Handle the events for UserControl3 in your WindowsFormsHost and raise your ConditionalClick event from the event handlers for the Windows Forms events by calling RaiseEvent(new RoutedEventArgs(ConditionalClickEvent)) :WindowsFormsHost中处理UserControl3的事件,并通过调用RaiseEvent(new RoutedEventArgs(ConditionalClickEvent))从 Windows Forms 事件的事件处理程序引发您的ConditionalClick事件:

public UserControl3Host()
{
    Child = userControl;
    userControl.label1.Click += (ss, ee) => RaiseEvent(new RoutedEventArgs(ConditionalClickEvent));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM