如何处理自定义控件中的事件

How to handle events in Custom Controls

我正在尝试创建一个简单的控制器,这是我的 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>

这是我背后的代码

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

我收到 The name MainButton does not exist in current context 错误。我也尝试设置 Drop = "myDrop",但是,那也没有用。

如何监听组件上的事件?

所以您的问题不是您设置的 'Drop' 值,而是对不可用的 MainButton 的引用。

我认为您正在尝试创建自定义控件,它通常定义为 UserControl,而不是 ResourceDictionary 的一部分。我可能是错的,因为我已经好几年没有使用 WPF 了。以下是创建用户控件的方法:

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

另请查看本教程:https://www.tutorialspoint.com/wpf/wpf_custom_controls.htm

按钮已通过 x:Name 属性分配了一个名称,以便我们能够在代码中找到它们并连接它们的点击事件处理程序。这是通过覆盖自定义控件 class.

中的 OnApplyTemplate 来完成的

从模板中检索按钮

public override void OnApplyTemplate() 
{ 
    Button mainButton = GetTemplateChild("MainButton") as Button; 
    if (mainButton != null) 
        mainButton.Click += MainBtnClick; 
}

遵循完整的教程,例如(但不一定):http://blog.magnusmontin.net/2013/03/16/how-to-create-a-custom-window-in-wpf/