简体   繁体   English

在Winforms中使用WPF自定义控件

[英]using a WPF custom control in Winforms

I am making a wpf custom control in Winforms. 我在Winforms中制作了wpf自定义控件。 I am using an element host to embedded the wpf control in to my Winforms. 我正在使用元素主机将wpf控件嵌入到Winforms中。

I have a few different ellipses in my wpf custom control and they all have a mouse down event handler, and in run time went i click on a ellipses, I can change their color , but my question is how do I seen that information to my winform application from my wpf? 我的wpf自定义控件中有几个不同的椭圆,它们都有鼠标按下事件处理程序,在运行时我单击了一个椭圆,我可以更改它们的颜色,但是我的问题是如何查看我的信息从我的WPF Winform应用程序?

I tried this : 我尝试了这个:

private void elementHost1_ChildChanged(object sender, System.Windows.Forms.Integration.ChildChangedEventArgs e)
    {
        int stop =0;
    }
  private void Ellipse_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Ellipse EP = sender as Ellipse;
        string tempS = EP.Name;
        EP.Fill = Brushes.Green;

        int you = 0;
    }
}

Also, event if the handler is called , I am not sure how to learn which Ellipse called it also, would there be a way of changing the Ellipse color from the Winform ? 另外,如果调用了处理程序,我不确定如何学习哪个Ellipse也调用了它,是否有办法从Winform更改Ellipse的颜色?

You can use events to do that. 您可以使用事件来做到这一点。 Create an event for your wpf control and raise it when clicked, then in winform add handler to that event and listen to the event. 为您的wpf控件创建一个事件,并在单击时引发它,然后在winform中向该事件添加处理程序并监听该事件。

for creating event: 用于创建事件:

public event Action OnEllipseClick;

for raising it: 为了提高它:

this.OnEllipseClick?.Invoke();

if winform for binding to event: 如果winform用于绑定事件:

this.userControl11.OnEllipseClick += UserControl11_OnEllipseClick;

private void UserControl11_OnEllipseClick()
{
    MessageBox.Show("hi");
}

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

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