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