【问题标题】:WPF numeric up down custom controlWPF 数字上下自定义控件
【发布时间】:2010-09-22 10:04:58
【问题描述】:

我一直需要为我的 WPF 应用程序使用数字上下控件。我阅读了此处发布的类似问题,并尝试使用此处提供的问题 > http://bot.codeplex.com/

我添加了引用并在我的 XAML 窗口中引用了它

xmlns:lib="clr-namespace:PixelLab.Wpf;assembly=PixelLab.Wpf"

并做到了。

<lib:NumericUpDown Name="year"></lib:NumericUpDown>

并不断收到错误消息:“nud”是一个未声明的命名空间。

我对 WPF 非常陌生,因此我们将不胜感激。

【问题讨论】:

  • 如果您收到该错误,则在您的代码中某处引用了“nud”。找到它并删除它或将其更改为“lib”。
  • 在整个项目中搜索“nud”,但没有找到任何匹配项。
  • 将项目(源代码)添加到您的应用程序中(而不是引用它),然后尝试调试控件初始化程序。这样你可能会得到更具体的错误。
  • 创建一个新的(临时)WPF 项目,将相同的引用添加到 PixelLab 库,在 XAML 中添加引用,并将 NumericUpDown 添加到窗口。看看能不能跑。如果它运行,则说明您项目中的某个地方存在问题。如果您遇到相同的错误,则可能是 PixelLab 库中的错误,因此您应该查看源代码。如果您在库中发现错误,请向他们报告,并在此处跟进,以帮助遇到相同问题的任何人。

标签: wpf wpf-controls custom-controls numericupdown


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    Vanilla XAML(无添加或包)实现:

        <Window x:Class="Spinner.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Spinner"
            mc:Ignorable="d"
            ResizeMode="CanMinimize" SizeToContent="WidthAndHeight" Title="Scroll Spinner">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <!-- The button exists just to have something other than the spinner be the object of focus. -->
                <Button Content="Reset" TabIndex="0"/>
                <!-- The spinner is just a scroll bar overlaying a text box (same tab indices). -->
                <!-- Only the scroll bar is named (in order to get its value); the text box just relfects the scroll bar's value. -->
                <TextBox GotFocus="TextBox_GotFocus" Grid.Row="1" Height="{Binding ElementName=SpinnerScr, Path=ActualHeight}" HorizontalAlignment="Stretch" IsReadOnly="True" TabIndex="1" Text="{Binding ElementName=SpinnerScr, Path=Value, StringFormat={}{0:####0}}" TextAlignment="Center"/>
                <ScrollBar x:Name="SpinnerScr" Background="Transparent" Focusable="True" Grid.Row="1" Height="20" LostFocus="SpinnerScr_LostFocus" Margin="0,3" Maximum="999" Orientation="Horizontal" SmallChange="1" TabIndex="1" Visibility="Hidden"/>
                <x:Code>
                    <![CDATA[
                    void SpinnerScr_LostFocus(object sender, RoutedEventArgs e) {
                        SpinnerScr.Visibility = Visibility.Hidden;
                    }
                    void TextBox_GotFocus(object sender, RoutedEventArgs e) {
                        SpinnerScr.Visibility = Visibility.Visible;
                        SpinnerScr.Focus();
                    }
                ]]>
                </x:Code>
            </Grid>
        </Window>
    
        using System.Windows;
        namespace Spinner {
            public partial class MainWindow : System.Windows.Window {
                public MainWindow() {
                    InitializeComponent();
                }
            }
        }
    

    当滚动条(或文本框)获得焦点时,滚动元素可见。失去焦点时,只有文本框可见。在任何代码隐藏中只能访问滚动条。

    【讨论】:

      【解决方案3】:

      只需将 TextBox 与垂直固定高度的 ScrollBar 组合起来,如下所示:

      <Grid Height="80">
                  <TextBox x:Name="part_TextBox" Text="{Binding Value,ElementName=part_Scrollbar,StringFormat={}{0:F6},Mode=TwoWay}" MaxLength="11" VerticalAlignment="Center" VerticalContentAlignment="Center" FontSize="24" Height="40"/>
                  <ScrollBar x:Name="part_Scrollbar" Orientation="Vertical" Minimum="0" Maximum="100" BorderBrush="{x:Null}" SmallChange="0.1" Height="32" Margin="8 4" VerticalAlignment="Stretch" Grid.Column="1" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Right">
                      <ScrollBar.RenderTransform>
                          <TransformGroup>
                              <ScaleTransform/>
                              <SkewTransform/>
                              <RotateTransform Angle="180"/>
                              <TranslateTransform/>
                          </TransformGroup>
                      </ScrollBar.RenderTransform>
                  </ScrollBar>
              </Grid>
      

      Maximum、Minimum 和 SmallChange/Increment 的绑定直接可用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多