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



我正在尝试创建一个简单的控制器,这是我的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",但是,这也不起作用。

如何侦听组件上的事件?

按钮已由 x:Name 属性分配了一个名称,以便我们能够在代码中找到它们并挂接它们的 click 事件处理程序。这是通过重写自定义控件类中的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/

因此,

您的问题不在于您设置为"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

最新更新