另一个XAML微调器...但存在问题 [英] Yet Another XAML Spinner ...but with an Issue

查看:61
本文介绍了另一个XAML微调器...但存在问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

万维网上有许多关于丢失"消息的页面. WPF微调器(数字下拉控件),可与WinForms一起使用.

The World Wide Web has more than a few pages about "missing" WPF spinners (numeric updown controls), available with WinForms.

这些变通办法有一些缺点:

There are some disadvantages to these work-arounds:

  • 某些安装不允许使用第二方软件包.
    因此,尽管CodePlex与Microsoft之间有着密切的联系,但Extended WPF Toolkit™是不可以".
  • 在XAML中实施,微调器并不是一件容易的事.
    这是前面提到的从头开始"的XAML代码的片段.文章:
    <ControlTemplate TargetType="{x:Type local:NumericUpDown}">
      <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Focusable="False">
        <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" VerticalAlignment="Center" Focusable="False">
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
          </Grid.ColumnDefinitions>
          <TextBox x:Name="PART_TextBox" VerticalAlignment="Center" HorizontalContentAlignment="Right"/>

  • Some installations do not allow the use of second party packages.
    Thus, in spite of the close association between CodePlex and Microsoft, the Extended WPF Toolkit™ is a "no no."
  • Implementing, in XAML, a spinner is not trivial.
    Here's a snippet of the XAML code from the aforementioned "from scratch" article:
    <ControlTemplate TargetType="{x:Type local:NumericUpDown}">
      <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Focusable="False">
        <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" VerticalAlignment="Center" Focusable="False">
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
          </Grid.ColumnDefinitions>
          <TextBox x:Name="PART_TextBox" VerticalAlignment="Center" HorizontalContentAlignment="Right"/>

我需要一个类似于Spinner的元素,即使它在外观上与WinForm版本并不相同,也可以识别.
另外,它还必须:

I needed a spinner-like element that, even though it is not visually identical to the WinForm version, would be recognizable.
Also, it had to:

    仅由香草"组成XAML
  • 不需要后备代码(C#或VB)
  • 占地面积很小,因此我可以在不增加任何项目占地面积的情况下应用它
  • Be composed of only "vanilla" XAML
  • Not require backing code (C# or VB)
  • Have a footprint so small that I could apply it without making extensive additions to any project's footprint

我想到的是a-l-m-o-s-t作品.这是Visual Studio 2015 Community Edition设计器窗口的屏幕截图的一部分:

在设计器视图中,您可以看到<TextBox>覆盖水平 <ScrollBar>.
看起来一切都很好
...
但是在执行时,应用程序看起来像这样:

<TextBox>包含<ScrollBar>,而不只是滚动条的 Track组件.

What I came up with a-l-m-o-s-t works. Here's part of a screen shot from a Visual Studio 2015 Community Edition designer window:

In the designer view, you can see the <TextBox> overlaying a horizontal <ScrollBar>.
All looks to be OK
...
but, on execution, the application looks like this:

The <TextBox> subsumes the <ScrollBar> rather than just the scrollbar's Track component.

SpinnerTXT.Width = SpinnerSCR.Track.ActualWidth;(仅一行)添加到窗口的加载事件处理程序(MainWindow_OnLoad)中,可以解决此问题,并根据需要显示滚动条.

Adding SpinnerTXT.Width = SpinnerSCR.Track.ActualWidth; (just one line) to the window's load event handler (MainWindow_OnLoad) corrects the problem and the scroller-spinner appears as desired.

Visual Studio设计人员喜欢"代码(适当地提供<TextBox>).
为什么纯XAML版本不起作用?

The Visual Studio designer "likes" the code (renders the <TextBox> appropriately).
Why does the XAML-only version not work?

如果有帮助,请参见下面的MainWindow.xaml.cs代码:
namespace Spinner {
  public partial class MainWindow : System.Windows.Window {
    public MainWindow() {
      InitializeComponent();
    }
    private void MainWindow_OnLoad(object sender, System.Windows.RoutedEventArgs e) {
      SpinnerTXT.Width = SpinnerSCR.Track.ActualWidth;
    }
  }
}

If it helps, here is the MainWindow.xaml.cs code:
namespace Spinner {
  public partial class MainWindow : System.Windows.Window {
    public MainWindow() {
      InitializeComponent();
    }
    private void MainWindow_OnLoad(object sender, System.Windows.RoutedEventArgs e) {
      SpinnerTXT.Width = SpinnerSCR.Track.ActualWidth;
    }
  }
}

App.xaml代码:
<Application x:Class="Spinner.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Spinner" StartupUri="MainWindow.xaml">
  <Application.Resources/>
</Application>

and the App.xaml code:
<Application x:Class="Spinner.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Spinner" StartupUri="MainWindow.xaml">
  <Application.Resources/>
</Application>

App.xaml.cs代码:
命名空间Spinner {
   公共局部类App:System.Windows.Application {}
}

and the App.xaml.cs code:
namespace Spinner {
    public partial class App : System.Windows.Application { }
}

有什么想法吗?
谢谢!

Any ideas?
Thanks!

WeBMartians

WeBMartians

推荐答案

您可以算出轨道的宽度并将其应用于文本框...

You could work out the width of the track and apply it to the textbox...

或者您也可以作弊"并设置宽度.

Or you could just "cheat" and set the width.

    <Grid>
        <ScrollBar 
            x:Name="SpinnerSCR" Height="20" Margin="10" Maximum="999"
                   Orientation="Horizontal" SmallChange="1"
                   Width="70"
                   />
        <TextBox x:Name="SpinnerTXT" 
                 Height="{Binding ActualHeight, ElementName=SpinnerSCR}"
                 IsReadOnly="True"
                 Text="{Binding ElementName=SpinnerSCR, Path=Value}"
                 TextAlignment="Center"
                 Width="40"
            />
    </Grid>

或者您可以在面板和尺寸两者中都放一个边距,在文本框上放一个边距:

Or you could put both in a panel and size that, put a margin on the textbox:

    <Grid>
        <Grid Width="70">
            <ScrollBar 
            x:Name="SpinnerSCR" Height="20"  Maximum="999"
                   Orientation="Horizontal" SmallChange="1"
                   />
            <TextBox x:Name="SpinnerTXT" 
                 Height="{Binding ActualHeight, ElementName=SpinnerSCR}"
                 IsReadOnly="True"
                 Text="{Binding ElementName=SpinnerSCR, Path=Value}"
                 TextAlignment="Center"
                 Margin="15,0,15,0"
            />
        </Grid>
    </Grid>


这篇关于另一个XAML微调器...但存在问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆