繁体   English   中英

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

[英]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错误The name MainButton does not exist in current context 我也尝试设置Drop = "myDrop" ,但是那也没有用。

如何收听组件上的事件?

因此,您的问题不是您要设置为“ Drop”值的原因,而是对MainButton的引用不可用。

我认为您正在尝试创建一个自定义控件,该UserControl通常定义为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属性为按钮分配了一个名称,以便我们能够在代码中找到它们并连接其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/

暂无
暂无

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

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