整数上下增加按钮

问题描述:

我有wpf工具包的integerupdown按钮.我只想使用向上按钮,而只是从显示中删除向下按钮.有什么办法吗?我只能想到让另一个用户控制文本框和按钮,但认为应该有一些东西 只是从相同的integerupdown控件中删除向下按钮.

I have a integerupdown button of wpf toolkit. i just want to use the up button and just remove the down button from display. is there any way to do it? i can only think of making another usercontrol of textbox and button but thought there should be something just to remove the down button from same integerupdown control.

根据 IntegerUpDown ,没有允许您想要的内容,您可以尝试使用Usercontrol.以下是一个简单的示例.

According to the IntegerUpDown, there is no property to allow what you want , you can try the Usercontrol. the following is a simple sample.

<UserControl x:Class="wpfAppDemo.wpfUserControl.IntegerUp"
             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:wpfAppDemo.wpfUserControl"
             mc:Ignorable="d" 
             d:DesignHeight="30" d:DesignWidth="250">

        <UserControl.Resources>
            <ControlTemplate x:Key="IntegerUp" TargetType="ContentControl">               
                <StackPanel Orientation="Horizontal">
                <TextBox x:Name="UpTextBox" Height="20" Width="200" HorizontalAlignment="Center" TextAlignment="Right"/>
                <Button x:Name="UpButton" Height="20" Width="40" Margin="0,0,0,0" Click="UpButton_Click">
                    <Canvas Width="40" Height="20" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
                        <Path Canvas.Left="11" Width="18" Height="18" Stretch="Fill"
              Fill="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}"
              Data="F1 M 35,19L 41,19L 41,35L 57,35L 57,41L 41,41L 41,57L 35,57L 35,41L 19,41L 19,35L 35,35L 35,19 Z">
                        </Path>
                    </Canvas>
                    <Button.Style>
                        <Style TargetType="Button">
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Foreground" Value="Red" />
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="False">
                                    <Setter Property="Foreground" Value="Gray" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>
            </StackPanel>
            </ControlTemplate>
        </UserControl.Resources>
    <ContentControl Height="30" x:Name="UpContentControl" Template="{StaticResource IntegerUp}"></ContentControl>
</UserControl>

public partial class IntegerUp : UserControl
    {
        public IntegerUp()
        {
            InitializeComponent();
        }

        private void UpButton_Click(object sender, RoutedEventArgs e)
        {
            TextBox textBox = UpContentControl.Template.FindName("UpTextBox", UpContentControl) as TextBox;
            int val;
            if (int.TryParse(textBox.Text, out val))
            {
                val++;
                textBox.Text = val.ToString();
            }
        }
    }

希望这对您有所帮助.

最好的问候,

鲍勃