WPF 精修篇 自定义控件

自定义控件 因为没有办法对界面可视化编辑 所以用来很少 

现在实现的是 自定义控件的 自定义属性 和自定义方法

用VS 创建自定义控件后 会自动创建 Themes 文件夹和 Generic.xaml 还有自定义的类 这边是SeachControl

Gneneric


    <Style TargetType="{x:Type local:SeachControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:SeachControl}">
                    <Grid>
                        <StackPanel Orientation="Horizontal" >
                            <TextBox Width="100" Height="20" Margin="0,0,5,0" Text="{Binding SearchText, RelativeSource={RelativeSource Mode=TemplatedParent},Mode=TwoWay}" Background="{TemplateBinding Background}"></TextBox>
                            <Button x:Name="button" Content="Select" Width="50" Height="20" ></Button>
                        </StackPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

自定义控件类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication24
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:WpfApplication24"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:WpfApplication24;assembly=WpfApplication24"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
    /// 并重新生成以避免编译错误:
    ///
    ///     在解决方案资源管理器中右击目标项目,然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:SeachControl/>
    ///
    /// </summary>
    public class SeachControl : Control
    {
        static SeachControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SeachControl), new FrameworkPropertyMetadata(typeof(SeachControl)));
        }



        public string SearchText
        {
            get { return (string)GetValue(SearchTextProperty); }
            set { SetValue(SearchTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SearchText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SearchTextProperty =
            DependencyProperty.Register("SearchText", typeof(string), typeof(SeachControl), new PropertyMetadata(""));

        public delegate void OnSeachClick(object ob, SearchEventArgs args);
        public event OnSeachClick SeachButtenClick;

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var button = GetTemplateChild("button");
            if (button is Button) 
            {
                (button as Button).Click += SeachControl_Click;
            }
        }

        void SeachControl_Click(object sender, RoutedEventArgs e)
        {
            if (SeachButtenClick != null) 
            {
                SeachButtenClick.Invoke(this, new SearchEventArgs() { SreachItem = SearchText });
            }
        }
        
    }
}

创建参数类

    public class SearchEventArgs:EventArgs
    {
        public string SreachItem { get; set; }
    }

 

main 引用 这里可以看到自定义的事件

<Window x:Class="WpfApplication24.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:MyNamespace="clr-namespace:WpfApplication24"
        Title="MainWindow" Height="350" Width="525">
	<Window.Resources>

 
	</Window.Resources>
    <Grid>
        <MyNamespace:SeachControl HorizontalAlignment="Center" SearchText="嗯嗯" VerticalAlignment="Center" Background="#FFCBCBCB" SeachButtenClick="SeachControl_SeachButtenClick"  />
    </Grid>
</Window>

Main的内容类

     private void SeachControl_SeachButtenClick(object ob, SearchEventArgs args)
        {
            MessageBox.Show("HI "+ args.SreachItem);
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小慧哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值