【问题标题】:Good NumericUpDown equivalent in WPF? [closed]WPF中好的NumericUpDown等价物? [关闭]
【发布时间】:2010-09-27 19:34:15
【问题描述】:

我正在 WPF 中寻找一个简单的 NumericUpDown(又名数字微调器)控件。这似乎是 WPF 中另一个缺乏控制的问题。肯定有一些现有的,我不想重新发明轮子。

【问题讨论】:

  • 你的意思是微软实际上已经提供了WPF控件?? :)
  • 这真是个糟糕的笑话。我开始拒绝搜索 wpf 控件,因为害怕找不到它们。
  • Xaml 唯一解决方案:stackoverflow.com/a/63734191/6859121

标签: wpf spinner numericupdown


【解决方案1】:

如果商业解决方案没问题,你可以考虑这个控制集: WPF Elements by Mindscape

它包含这样一个旋转控件,或者(我个人偏好)一个旋转装饰器,可以像这样在 XAML 中装饰各种数字控件(如 IntegerTextBox、NumericTextBox,也是控件集的一部分):

<WpfElements:SpinDecorator>
   <WpfElements:IntegerTextBox Text="{Binding Foo}" />
</WpfElements:SpinDecorator>

【讨论】:

    【解决方案2】:

    添加文本框和滚动条

    在 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
    

    【讨论】:

      【解决方案3】:

      扩展的 WPF 工具包有一个:NumericUpDown

      【讨论】:

      • 更新:扩展的 WPF 工具包用这些 DecimalUpDown、DoubleUpDown 或 IntegerUpDown 替换了该控件。
      • 仅供参考,Xceed 宣布 [github.com/xceedsoftware/wpftoolkit](they 将继续在 MS-PL 下发布 3.8.x 版本,直到 2020 年 12 月 31 日)。 “这将是在此类许可下发布的最后一个版本,下一个版本将在我们的新社区许可下发布,仅允许非商业用途。”
      【解决方案4】:

      原始 WPF 控件集中缺少但经常使用的控件是 NumericUpDown 控件。这是让用户在小范围内从固定范围内选择数字的一种巧妙方法。可以使用滑块,但对于水平空间很小的紧凑型表单,NumericUpDown 是必不可少的。

      解决方案 A(通过 WindowsFormsHost)

      您可以在 WPF 中使用 Windows 窗体 NumericUpDown 控件,方法是将其托管在 WindowsFormsHost 中。请注意,您必须包含对 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 版本,但都涉及安装 3rd 方 dll 和项​​目开销。构建自己的要简单得多,而且使用 ScrollBar 是一种有目的的方法。

      没有 Thumb(只有中继器按钮)的垂直 ScrollBar 实际上正是我们想要的。它继承了 rom RangeBase,因此它具有我们需要的所有属性,例如 Min、Max 和 SmallChange(设置为 1,将其限制为 Integer 值)

      所以我们改变了 ScrollBar ControlTemplate。首先,我们删除 Thumb 和 Horizo​​ntal 触发动作。然后我们将剩余部分分组到一个网格中,并为数字添加一个 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>
      

      来源:

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
      猜你喜欢
      • 1970-01-01
      • 2010-12-01
      • 2023-03-09
      • 2011-03-20
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多