WPF 自定义控件构造、触发器和事件 [英] WPF Custom Controls Construction,Triggers and Events

查看:67
本文介绍了WPF 自定义控件构造、触发器和事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个新的自定义控件.我发现很少有教程可以让我了解如何实现这一目标.据我了解,创建新的自定义控件总是通过扩展当前控件来完成的,甚至可以扩展来自层次结构非常基本级别的控件,例如,您甚至可以扩展:

I want to build a new custom control. I found few tutorials that gave me some clue of how to achieve this. As I understand it, creating a new custom control is always done by extending a current one, and it is even possible to extend a control from the very basic levels of the hierarchy, in example, you can even extend:

  • 界面元素
  • 框架元素
  • 控制
  • 内容控制
  • HeaderedContentControl
  • ItemsControl
  • 选择器
  • RangeBase

如以下教程中所述:http://wpftutorial.net/HowToCreateACustomControl.html

所以,我按照教程创建了一个自定义控件库类型的新项目,得到了我的通用 .xaml 和我的代码.到目前为止一切顺利.

So, I followed the tutorial and created a new project of type Custom Control Library, got my generic .xaml and my code behind. So far so good.

我可以区分 3 种类型或类别的事件.

There are 3 types or categories of events I can distinguish between.

  1. 由将使用我的控件的窗口(或容器)消耗的事件:这些是我想向外部公开的事件.怎么写?

  1. Events that are consumable by the window (or container) that will use my control: those are the events I would like to expose to the outside. How to write those?

与控件本身相关且未暴露于外部的事件;例如,如果鼠标悬停在我的控制上并且我想做出反应到那个.我可以在 XAML 中做到这一点:

Events that are related to the control itself and not being exposed outside; for example, if the mouse is over my control and I would like to react to that. I can do that in the XAML:

<ControlTemplate.Triggers>
  <Trigger Property="IsMouseOver" Value="true">  
    <Setter Property="Fill" TargetName="LeftTraingularIndicator">  
      <Setter.Value>  
        <SolidColorBrush Color="Yellow" />  
      </Setter.Value>  
    </Setter>
  </Trigger>
</ControlTemplate.Triggers>  

假设我有一个带有 fill 属性的元素在我的名为 x:name="LeftTraingularIndicator"

Assuming of couse I have an element with the fill property in my ControlTemplate which is named x:name="LeftTraingularIndicator"

问题:

现在我想在我的 XAML 中对 IsMouseDown 做出反应.我怎么做?没有IsMouseDown"触发器.此外,如果我想要在后面的代码中做出反应?甚至更进一步如果我想要从后面的代码中更改 LeftTraingularIndicator "Fill"?

Now I want to react in my XAML to IsMouseDown. How do I do that? There is no "IsMouseDown" Trigger. Furthermore what if I want to react to that in the code behind? And even further what if I want to change LeftTraingularIndicator "Fill" from the code behind?

  1. 与属于视觉对象的子元素相关的事件构建我的 ControlTemplate,例如如果我想做出反应怎么办到 XAML/甚至代码隐藏中LeftTraigularIndicator"的IsMouseOver"?甚至可能两者兼而有之.

我现在尝试了 2 天......感觉我在理解事物的运作方式方面缺少一些东西.没有找到深入解释这些问题的教程.

I am attempting for 2 days now... feeling I am missing something in my understanding of how things work. Didn't find any tutorial that deeply explains those questions.

对于我在此处提出的每个问题,我想查看几行示例.

I would like to see a few lines of example for each of the questions I surfaced here.

谢谢.

推荐答案

经过广泛研究...

1) 向外界公开事件...就像你从任何其他课程中暴露一样.

1) Exposing events to the outside... Simply as if you were exposing from any other class.

public delegate void myDelegate(int someValue);  
public event myDelegate myEvent;

以及代码中的某处:

if(myEvent!=null)
  myEvent(5);

那部分没有什么新东西.

Nothing new in that part.

2) 从后面的代码中创建一个实例构造函数

2) From the code behind create an instance constructor

public MyCustomControl()
{
    MouseMove += MyCustomControl_MouseMove;
}


void MyCustomControl_MouseMove(object sender, MouseEventArgs e)
{
   //now you can react to the movement of the mouse.
   //if for example I want to address an element, let's say a rectangle:

   var ele = (Rectangle)Template.FindName("myRect",this);
   ele.Fill=myNewBrush;

   // provided that we have an element named "myRect" (x:name="myRect") in
   // the generic.xaml style->Control Template-> which corresponds to that name.

}

3) 不推荐 - 因为它属于用户控件的范围而不是自定义控件.自定义控件是原子",用户控件更适合目的组合控件.

3) Not recommended - as it belong to the scope of user-controls and not custom controls. Custom controls are the "atoms", user controls are more suitable to the purpose of combining controls.

但并非不可能...

var myButton = (Button)Template.FindName("myButton",this);
myButton.OnMouseMove += ....

请记住:

  • 任何应该在代码隐藏中知道的东西都必须被命名.

  • Anything that should be known in the code-behind must be named.

您的 xaml 应该不知道背后的代码是做什么的.(除了! - 继续阅读)

Your xaml should have no knowledge of what the code behind do. (except! - keep reading)

代码隐藏中应该知道的部分必须具有正确的名称您设计 XAML.

Parts that should be known in the code-behind must have the correct name when you design your XAML.

我真的希望这能帮助那些在尝试开发时遇到墙"的人他们自己的自定义控件.

I truly hope this will help others who get a "wall" when trying to develop their own custom-controls.

这篇关于WPF 自定义控件构造、触发器和事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆