天天看點

wpf給自定義控件定義事件

wpf給自定義控件定義事件

首先項目中添加​

​UserControl.xaml​

​​,名稱改為​

​MyUserButton.xaml​

​。

修改​

​xaml​

​檔案如下:

Button x:Class="UserButtonTest.MyUserButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:UserButtonTest"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Background="Transparent" BorderThickness="0" x:Name="dd">

    <Button.Template>
        <ControlTemplate>
            <Grid>
                <Border Name="border" BorderThickness="3" CornerRadius ="5" Background="#FFFFCC" BorderBrush="#FF6633">
                    <Border BorderThickness="0" Height="20">
                        <Viewbox VerticalAlignment="Center" HorizontalAlignment="Center">
                            <TextBlock Name="tb" Text="{Binding RelativeSource={x:Static RelativeSource.Self},Path=Text}">ee</TextBlock>
                        </Viewbox>
                    </Border>
                </Border>
            </Grid>
        </ControlTemplate>
    </Button.Template>

</Button>      

修改對應的​

​.cs​

​檔案如下:

namespace UserButtonTest
{
    /// <summary>
    /// MyUserButton.xaml 的互動邏輯
    /// </summary>
    public partial class MyUserButton : Button
    {
        public MyUserButton()
        {
            InitializeComponent();            
        }
    }
 }      

對應的​

​.cs​

​檔案中添加Click事件

public static readonly RoutedEvent clickEvent =
             EventManager.RegisterRoutedEvent("ClickF", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyUserButton));

        public event RoutedEventHandler ClickF
        {
            add
            {
                AddHandler(clickEvent,value);
            }

            remove
            {
                RemoveHandler(clickEvent, value);
            }
        }

        protected override void OnClick()
        {
            RoutedEventArgs args = new RoutedEventArgs(clickEvent, this);
            RaiseEvent(args);
        }      

然後在​

​MainWindow.xaml​

​中引用這個控件

<Window x:Class="UserButtonTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:UserButtonTest"關鍵是引入這句
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <local:MyUserButton Width="90" Height="50" ClickF="MyUserButton_ClickF">

        </local:MyUserButton>
    </Grid>
</Window>      

之後在​

​MainWindow.cs​

​​檔案中定義​

​MyUserButton_ClickF​

​事件

private void MyUserButton_ClickF(object sender, RoutedEventArgs e)
 {
   MessageBox.Show("hi");
 }      

繼續閱讀