自定义控件,写一个对外事件,并使用,这样方便在控件使用过程中,添加一些业务上的逻辑判断,方便控件的使用

随便写的,很简单,基本上全部是代码,部分文字注释,欢迎讨论
ps:如有转载,请说明代码来源,谢谢!

1.添加了一个UserControl的windows应用程序,用来放封装的控件

xml界面代码,随便用了一个文本框和一个button

<UserControl x:Class="UserControl.TextBtn"
             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" 
             d:DesignHeight="28" d:DesignWidth="200" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="30"/>
        </Grid.ColumnDefinitions>
            <TextBox Grid.Row="0" Grid.Column="0" x:Name="TextInput"
                     Text="{Binding Text}"
                     AcceptsReturn="True"
                     Loaded="TextInput_OnLoaded"
                     InputMethod.IsInputMethodEnabled="False"/>
        <Button x:Name="PopBtn" Width="30" Content="..." Grid.Row="0" Grid.Column="1" Click="PopBtn_OnClick"/>
    </Grid>
</UserControl>

xml.cs代码,很简单的委托、事件和依赖属性构成

using System.Windows;
using System.Windows.Controls;

namespace UserControl
{
    /// <summary>
    /// TextBtn.xaml 的交互逻辑
    /// </summary>
    public partial class TextBtn
    {
        public TextBtn()
        {
            InitializeComponent();
        }

        #region 对外事件
        public delegate void BeforeClick(object sender, RoutedEventArgs e);
        public event BeforeClick MyRoutedEvent;
        public static readonly DependencyProperty MyRoutedEventProperty = DependencyProperty.Register(
            "MyRoutedEvent", typeof(BeforeClick), typeof(TextBtn), new FrameworkPropertyMetadata(null, null));

        public delegate void ExpandMethod();
        public event ExpandMethod ExpandMethodEvent;
        public static readonly DependencyProperty ExpandMethodEventProperty = DependencyProperty.Register(
            "ExpandMethodEvent", typeof(ExpandMethod), typeof(TextBtn), new FrameworkPropertyMetadata(null, null));
        #endregion

        #region 属性
        public bool isFocus
        {
            get { return (bool)GetValue(isFocusProperty); }
            set { SetValue(isFocusProperty, value); }
        }

        public static readonly DependencyProperty isFocusProperty = DependencyProperty.Register(
            "isFocus", typeof (bool), typeof (TextBtn), new FrameworkPropertyMetadata(false,OnIsFocusChanged));

        public string Text
        {
            get { return (string) GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

          public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
            "Text", typeof (string), typeof (TextBtn), new PropertyMetadata(default(string)));

        private static void OnIsFocusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var element = d as TextBtn;
            if (element != null)
            {
                if ((bool) e.NewValue)
                {
                    element.TextInput.Focus();
                    element.TextInput.SelectAll();
                }
            }
        }
        /// <summary>
        /// 扩展字段
        /// </summary>
        public string expendMain { get; set; }

        /// <summary>
        /// 判断能否拼接
        /// </summary>
        public bool resultPopClick { get; set; }
        #endregion

        private void TextInput_OnLoaded(object sender, RoutedEventArgs e)
        {
            var obj = sender as TextBox;
            if (obj == null) return;
            if (isFocus)
            {
                obj.Focus();
                obj.SelectAll();
            }
        }

        private void PopBtn_OnClick(object sender, RoutedEventArgs e)
        {
            if (MyRoutedEvent != null)
            {
                resultPopClick = true;
                MyRoutedEvent(sender, e);
                if (!resultPopClick)
                    return;
            }
            if (string.IsNullOrWhiteSpace(TextInput.Text))
            {
                TextInput.Text = string.Empty;
                e.Handled = true;
                return;
            }
            expendMain = TextInput.Text.Trim();
            if(ExpandMethodEvent!=null) ExpandMethodEvent();
        }
    }
}

2.建立一个项目,引用usercontrol,直接使用封装的控件

调用的xml代码:

 <Window x:Class="Main.TextBtnWindow"
             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:uc="clr-namespace:UserControl;assembly=UserControl"
             Height="250" Width="250" ResizeMode="NoResize">
    <Grid >
        <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Margin="5,0,0,0">
            <TextBox x:Name ="InputTB" Width="150" Height="30" Margin="0,20,0,0" InputMethod.IsInputMethodEnabled="False"/>
            <uc:TextBtn x:Name="TextBtn" Height="28" Width="150" Margin="0,20,0,0" isFocus="True" MyRoutedEvent="TextBtn_OnMyRoutedEvent" ExpandMethodEvent="TextBtn_OnExpandMethodEvent"/>
            <TextBox x:Name ="ResultTB" Width="150" Height="30" Margin="0,20,0,0" IsReadOnly="True"/>
        </StackPanel>
    </Grid>
</Window>

调用的xml.cs代码:

using System;
using System.Windows;
namespace Main
{
    /// <summary>
    /// TextBtnWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TextBtnWindow
    {
        public TextBtnWindow()
        {
            InitializeComponent();
        }

        private void TextBtn_OnMyRoutedEvent(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(InputTB.Text))
            {
                MessageBox.Show("被转换字符不能为空!");
                TextBtn.resultPopClick = false;
                e.Handled = true;
                return;
            }
        }

        private void TextBtn_OnExpandMethodEvent()
        {
            ResultTB.Text = InputTB.Text + TextBtn.expendMain;
        }
    }
}

3.程序运行的效果:

这个其实就是一个简单的字段拼接,但是在点击button的时候,回去判断拼接的头信息是否为空。像这样的判断属与业务上的判断,最好不要写到控件中间的逻辑里面去,所以就在封装控件的时候,添加一个对外的事件,用来进行一些逻辑上的业务判断,这样保证控件里面的代码更加纯粹一点。当然哈,这个都是个人的一些看法,如果有更好的方法,希望留言讨论,本人学习时间较短,基本上都是自己研究,很多地方逻辑都不完善,希望指正!
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值