[英]How to handle events in Custom Controls
I am trying to create a simple controller, here is my Generic.xaml; 我正在尝试创建一个简单的控制器,这是我的Generic.xaml;。
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TopluNotEkle">
<Style TargetType="{x:Type local:FileSelector}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:FileSelector}">
<Button BorderThickness="8,8,8,8" Background="Gray" x:Name="MainButton">
<Button.BorderBrush>
<DrawingBrush TileMode="Tile" Viewport="0,0,64,64" ViewportUnits="Absolute">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="LightGray">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,100,100" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing Brush="DarkGray">
<GeometryDrawing.Geometry>
<GeometryGroup>
<RectangleGeometry Rect="0,0,50,50"/>
<RectangleGeometry Rect="50,50,50,50"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Button.BorderBrush>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="BlueViolet" FontWeight="Bold" FontSize="20">Drag & Drop</TextBlock>
</Button>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
and this is my code behind 这是我的代码背后
public class FileSelector : Control
{
static FileSelector()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(FileSelector), new FrameworkPropertyMetadata(typeof(FileSelector)));
MainButton.Drop += myDrop;
}
static void myDrop(object sender, DragEventArgs e)
{
Debug.Fail("This is called");
}
}
I am getting The name MainButton does not exist in current context
error. 我正在获取
The name MainButton does not exist in current context
错误The name MainButton does not exist in current context
。 I also tried setting Drop = "myDrop"
, however, that also didn't work. 我也尝试设置
Drop = "myDrop"
,但是那也没有用。
How can I listen to events on my component? 如何收听组件上的事件?
So your problem is not what you are setting as a 'Drop' value, it is the reference to the MainButton
that is not available. 因此,您的问题不是您要设置为“ Drop”值的原因,而是对
MainButton
的引用不可用。
I think you are trying to create a custom control, which typically defined as UserControl
, not as a part of ResourceDictionary
. 我认为您正在尝试创建一个自定义控件,该
UserControl
通常定义为UserControl
,而不是ResourceDictionary
的一部分。 I might be wrong as I haven't worked with WPF for several years. 我可能已经错了,因为我已经在WPF工作多年了。 Here is how to create user control:
这是创建用户控件的方法:
<UserControl x:class= "YourNamespace.FileSelector" ... other xmlns garbage>
<Button BorderThickness="8,8,8,8" Background="Gray" x:Name="MainButton">
<Button.BorderBrush>
<DrawingBrush TileMode="Tile" Viewport="0,0,64,64" ViewportUnits="Absolute">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="LightGray">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,100,100" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing Brush="DarkGray">
<GeometryDrawing.Geometry>
<GeometryGroup>
<RectangleGeometry Rect="0,0,50,50"/>
<RectangleGeometry Rect="50,50,50,50"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Button.BorderBrush>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="BlueViolet" FontWeight="Bold" FontSize="20">Drag & Drop</TextBlock>
</Button>
Also check out this tutorial: https://www.tutorialspoint.com/wpf/wpf_custom_controls.htm 另请查看本教程: https : //www.tutorialspoint.com/wpf/wpf_custom_controls.htm
Buttons have been assigned a name by the x:Name
attribute for us to be able to find them in code and hook up their click event handlers. 已通过
x:Name
属性为按钮分配了一个名称,以便我们能够在代码中找到它们并连接其click事件处理程序。 This is done by overriding the OnApplyTemplate
in our Custom Control class. 这是通过覆盖我们的“自定义控件”类中的
OnApplyTemplate
来完成的。
Retrieve the Button from the template 从模板中检索按钮
public override void OnApplyTemplate()
{
Button mainButton = GetTemplateChild("MainButton") as Button;
if (mainButton != null)
mainButton.Click += MainBtnClick;
}
Follow a complete tutorial like for example (but not necessarily) this: http://blog.magnusmontin.net/2013/03/16/how-to-create-a-custom-window-in-wpf/ 遵循完整的教程,例如(但不一定): http : //blog.magnusmontin.net/2013/03/16/how-to-create-a-custom-window-in-wpf/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.