Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向自定义控件添加功能_C#_Wpf - Fatal编程技术网

C# 向自定义控件添加功能

C# 向自定义控件添加功能,c#,wpf,C#,Wpf,我需要一些关于如何添加功能到我的自定义控件的建议,它只是一个文本框和两个按钮,增加和减少它的值,这将是数字 我不明白的是为这个功能添加代码的最佳方法。我应该在自定义控制代码中使用命令吗?以下是我目前掌握的代码: XAML-Generic.XAML <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.micro

我需要一些关于如何添加功能到我的自定义控件的建议,它只是一个文本框和两个按钮,增加和减少它的值,这将是数字

我不明白的是为这个功能添加代码的最佳方法。我应该在自定义控制代码中使用命令吗?以下是我目前掌握的代码:

XAML-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:Controls">

    <Style TargetType="{x:Type local:NumericUpDown}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:NumericUpDown}">
                    <Grid HorizontalAlignment="Center" Margin="5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>

                        <Button Grid.Column="0" 
                                Padding="5"
                                Background="Gray"
                                Command="{Binding Source=NumBox, Path=Increase}">
                            <Button.Content>
                                <Path Data="M0,0 L1,0 0.5,1Z" 
                                      Width="6" 
                                      Height="6" 
                                      Fill="White"
                                      Stretch="Fill"/>
                            </Button.Content>
                        </Button>
                        <TextBox x:Name="NumBox" 
                                 Grid.Column="1" 
                                 Text="0" 
                                 Padding="2" />
                        <Button Grid.Column="2" 
                                Padding="5"
                                Background="Gray">
                            <Button.Content>
                                <Path Data="M0,1 L1,1 0.5,0Z" 
                                      Width="6" 
                                      Height="6" 
                                      Fill="White" 
                                      Stretch="Fill" />
                            </Button.Content>
                        </Button>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

显然,我还没有让命令运行,决定在继续之前征求意见

这取决于用户单击按钮时要执行的操作。 如果功能仅用于增加和减少文本框中的值。您可以注册到代码隐藏上的命令单击事件(这是您的逻辑,您不想添加更多动态逻辑)


如果您还想让用户在单击按钮时有机会添加其自定义功能,那么您也可以也添加命令大多数情况下,当遇到自定义控件时,您会像这样覆盖OnApplyTemplate()方法。(记住命名按钮)


谢谢,我以后会记住这一点,但是第一个答案适合我的需要。
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Controls
{
    public class NumericUpDown : Control
    {
        static NumericUpDown()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDown), 
                new FrameworkPropertyMetadata(typeof(NumericUpDown)));
        }
    }
    public static class Command
    {
        public static readonly RoutedUICommand Increase = new RoutedUICommand("Increase the number", "IncreaseCommand", typeof(Command));
    }

    public class IncreaseCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return false;
        }
        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            MessageBox.Show("click!");
        }
    }
}
public class NumericUpDown : Control
{
    static NumericUpDown()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDown), 
            new FrameworkPropertyMetadata(typeof(NumericUpDown)));
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        Button increaseButton = this.GetTemplateChild("increaseButton") as Button;
        if (increaseButton != null)
        {
            increaseButton.Click += increaseButton_Click;
        }
    }

    private void increaseButton_Click(object sender, RoutedEventArgs e)
    {
        // Do what you want to do.
    }
}