WPF中好用的NumericUpDown控件是什么?

103

我正在寻找一个简单的WPF NumericUpDown(又称数字微调器)控件。在WPF中,这似乎是另一个缺失的控件。肯定有一些现有的控件,我不想重复造轮子。


25
你的意思是微软真的提供了WPF控件吗? :) - BobbyShaftoe
这真是一个糟糕的玩笑。我变得对搜索wpf控件产生了抵触,因为我担心找不到它们。 - MadeOfAir
仅限Xaml解决方案:https://dev59.com/NXRA5IYBdhLWcg3wvgsb#63734191 - Mr. Squirrel.Downy
4个回答

100

26
更新:扩展的 WPF 工具包用 DecimalUpDown、DoubleUpDown 或 IntegerUpDown 替换了该控件。 - Erez
4
FYI,Xceed 宣布[https://github.com/xceedsoftware/wpftoolkit]他们将继续在 2020 年 12 月 31 日之前使用 MS-PL 发布版本 3.8.x。这将是最后一个在此许可证下发布的版本,下一个版本将在我们的新社区许可证下发布,该许可证仅允许非商业使用。 - skst

3

原始的WPF控件集中缺少但广泛使用的控件是NumericUpDown控件。它是一种简洁的方式,可以让用户在一个小区域内选择一个固定范围内的数字。虽然可以使用滑块,但对于水平空间有限的紧凑表单来说,NumericUpDown是必不可少的。

解决方案A(通过WindowsFormsHost)

您可以通过在WindowsFormsHost中托管它来在WPF中使用Windows Forms NumericUpDown控件。请注意,您必须包括对System.Windows.Forms.dll程序集的引用。

<Window x:Class="WpfApplication61.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" 
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>    
        <WindowsFormsHost>
            <wf:NumericUpDown/>
        </WindowsFormsHost>
...

B方案(自定义)

现有一些商业和Codeplex版本,但都涉及到将第三方dll和开销安装到您的项目中。更简单的方法是构建自己的版本,使用ScrollBar是一个简单的方法。

一个没有Thumb(只有重复按钮)的垂直ScrollBar实际上就是我们想要的。它继承自RangeBase,因此具有我们需要的所有属性,例如Min、Max和SmallChange(设置为1,以限制其为整数值)。

所以我们改变ScrollBar ControlTemplate。首先,我们删除Thumb和水平触发器动作。然后,我们将剩余部分分组到网格中,并添加一个TextBlock来显示数字:

<Grid Margin="2">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <TextBlock VerticalAlignment="Center" FontSize="20" MinWidth="25" Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}}"/>
    <Grid Grid.Column="1" x:Name="GridRoot" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Background="{TemplateBinding Background}">
        <Grid.RowDefinitions>
            <RowDefinition MaxHeight="18"/>
            <RowDefinition Height="0.00001*"/>
            <RowDefinition MaxHeight="18"/>
        </Grid.RowDefinitions>
        <RepeatButton x:Name="DecreaseRepeat" Command="ScrollBar.LineDownCommand" Focusable="False">
            <Grid>
                <Path x:Name="DecreaseArrow" Stroke="{TemplateBinding Foreground}" StrokeThickness="1" Data="M 0 4 L 8 4 L 4 0 Z"/>
            </Grid>
        </RepeatButton>
        <RepeatButton Grid.Row="2" x:Name="IncreaseRepeat" Command="ScrollBar.LineUpCommand" Focusable="False">
            <Grid>
                <Path x:Name="IncreaseArrow" Stroke="{TemplateBinding Foreground}" StrokeThickness="1" Data="M 0 0 L 4 4 L 8 0 Z"/>
            </Grid>
        </RepeatButton>
    </Grid>
</Grid>

参考资料:


1

添加一个文本框和滚动条

在VB中

Private Sub Textbox1_ValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of System.Double)) Handles Textbox1.ValueChanged
     If e.OldValue > e.NewValue Then
         Textbox1.Text = (Textbox1.Text + 1)
     Else
         Textbox1.Text = (Textbox1.Text - 1)
     End If
End Sub

1
如果商业解决方案可行,您可以考虑使用此控件集: Mindscape的WPF元素 它包含旋转控件和旋转装饰器(我个人更喜欢),可以像这样在XAML中装饰各种数字控件(如IntegerTextBox、NumericTextBox,也是该控件集的一部分):
<WpfElements:SpinDecorator>
   <WpfElements:IntegerTextBox Text="{Binding Foo}" />
</WpfElements:SpinDecorator>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接