WPF custom control production

  Because sometimes you need customized controls, need to combine and integrate multiple complex functions of control, so you can consider a custom user control. Sharing a simple numerical decrease following functions described as custom control.

The effect is as follows:

 

1. Create a custom user control (Add -> New Item -> User Control)

 

2, written in XAML

<UserControl x:Class="XXX.自定义控件.MyNumericUpDown"
             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:XXX.自定义控件"
             mc:Ignorable="d" 
             d:DesignHeight="30" d:DesignWidth="120">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="40*"/>
            <ColumnDefinition Width="40*"/>
        </Grid.ColumnDefinitions>

        <TextBox Name="TextBox_Num" Grid.Column="0" Text="1" FontSize="20" TextAlignment="Center" MinWidth="40" VerticalContentAlignment="Center"/>
        <Button Name="Button_Add" Grid.Column="1" Content="" Click="Button_Add_Click" Background="Aqua"/>
        <Button Name="Button_Sub" Grid.Column="2" Content="" Click="Button_Sub_Click" Background="Aqua"/>
        
    </Grid>
</UserControl>

UI is simple, I will not explain ...

 

2, write code behind

 /// <summary>
    /// MyNumericUpDown.xaml 的交互逻辑
    /// </summary>
    public partial class MyNumericUpDown : UserControl
    {

        /// <summary>
        /// 当前值
        /// </summary>
        public int Num
        {
            get
            {
                int value = 0;
                this.Dispatcher.Invoke(new Action(() =>
                    value = Convert.ToInt32(this.TextBox_Num.Text.Trim())
                ));
                return value;
            }
            set
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    this.TextBox_Num.Text = value.ToString();
                }));                
            }
        }

        public MyNumericUpDown()
        {
            InitializeComponent();
        }
        
        private void Button_Add_Click(object sender, RoutedEventArgs e)
        {
            int num = int.Parse(this.TextBox_Num.Text.Trim());
            if (num > 0)
            {
                this.TextBox_Num.Text = (num + 1).ToString();
            }
        }

        private void Button_Sub_Click(object sender, RoutedEventArgs e)
        {
            int num = int.Parse(this.TextBox_Num.Text.Trim());
            if (num > 0)
            {
                if ((num - 1) == 0)
                    return;
                this.TextBox_Num.Text = (num - 1).ToString();
            }
        }
    }

Logic is relatively simple, two button click event, each time plus one or minus one. It should be noted that the disclosed properties (only a value of the property is the embodiment disclosed control the Num), the properties required for packaging of the reader, and the reader is provided when the TextBox control operation callback Invoke UI thread is preferably required to operate, otherwise error (of course, you can also Invoke outside) when the other thread operations.

 

3, control calls

User control inherits UserControl, UserControl inherits from ContentControl, you can call the same general controls, but due to the inconsistent namespace, you declare the namespace.

xmlns: Z = " CLR namespace-:. custom XXX " // Windows namespace declaration tag 
<Z: MyNumericUpDown X: the Name = " MyNumericUpDown_PageNum " the Width = " 120 " the Height = " 30 " > </ Z: MyNumericUpDown >

 

In fact, the best we can attribute to the need for the external data binding written dependency property, so that we can have a dependency property characteristics (such as binding), but more complex, next time specify.

 

Guess you like

Origin www.cnblogs.com/xiaomengshan/p/11564474.html