用于数据输入的基本WPF窗口功能

表的内容 介绍使用代码的用户体验获取wpfwindowslib历史 介绍 在大多数程序中,有一些窗口用户必须输入一些数据,控件应该验证这些数据。只有输入了所有必需的数据,用户才能保存它。如果他更改了一些数据,但还没有保存,那么当他关闭窗口时,就会收到警告,说他可能会丢失一些数据。如果所有的功能都被自动添加到你所有的窗口中,而不需要你编写太多的程序,那不是很好吗? WpfWindowsLib提供了这个功能。本文描述了它的功能以及如何使用它。WpfWindowsLib是为。net Core 3.1及更高版本编写的。 用户体验 这可能不是您所见过的最漂亮的窗口,但这里的思想是系统地向用户展示各种控件在不同状态下是如何显示的。在每一行中,相同的控件类型显示3次。在第一列中,每个控件都为空。在第二列中,控件也是空的,但是用户必须在按Save按钮之前输入一些数据。在第三列中,控件有一些初始数据。 用户现在至少需要填写所有必需的字段。只有这样,Save按钮才会启用。一旦他按下保存按钮,它将再次被禁用。如果用户随后更改了任何数据,则会再次启用Save按钮。已启用的Save按钮告诉用户他已经更改了一些数据。 当用户在保存更改之前试图关闭窗口时,会发生什么情况? 他会收到一条警告消息,窗口会向他显示他已经更改但尚未保存的数据。然后,他可以决定是关闭窗口并放弃更改,还是继续数据输入并保存更改。 使用的代码 您几乎不需要编写任何代码就可以获得所有这些功能。WpfWindowsLib库提供以下控件: 知道他们的数据什么时候改变了知道他们的数据什么时候改变了(用户撤销了他的改变)知道什么时候一个“必需的”控件缺少数据知道什么时候一个“必需的”控件有数据自动找到他们所在的窗口并通知窗口关于每个状态改变 该窗口必须继承WpfWindowsLib中的CheckedWindow。 隐藏,复制Code

<wwl:CheckedWindowx:Class="Samples.SampleWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:wwl="clr-namespace:WpfWindowsLib;assembly=WpfWindowsLib">
  <StackPanel>
    <wwl:CheckedTextBoxx:Name="TestCheckedTextBox"MinWidth="100"MaxLength="20"IsRequired="True"/>
    <Buttonx:Name="SaveButton"Content="_Save"/>
  </StackPanel>
</wwl:CheckedWindow>

CheckedTextBox继承自TextBox,添加ICheck功能(见下面的解释)和IsRequired属性。当该值设置为true时,用户必须提供一个值(即。, Text.Length>0)在保存按钮可用之前: 在XAML,也在后面的代码,窗口必须继承从CheckedWindow: 隐藏,收缩,复制Code

using System.Windows;
using WpfWindowsLib;

namespace Samples {
  public partial class SampleWindow: CheckedWindow {
    public SampleWindow() {
      InitializeComponent();

      //write some code here to display data coming from a database, etc.
      TestCheckedTextBox.Text = database.Read(...);
      SaveButton.Click += saveButton_Click;
      updateSaveButtonIsEnabled();
    }

    private void saveButton_Click(object sender, RoutedEventArgs e) {
      //write some code here to save the data the user has entered
      database.Write(..., TestCheckedTextBox.Text);
      Close();
    }

    private void updateSaveButtonIsEnabled() {
      SaveButton.IsEnabled = HasICheckChanged && IsAvailable;
    }

    protected override void OnICheckChanged() {
      updateSaveButtonIsEnabled();
    }

    protected override void OnIsAvailableChanged() {
      updateSaveButtonIsEnabled();
    }
  }
}

只有非常少的代码需要添加到该窗口: Save按钮 窗口的外观没有任何要求。但最有可能的是,会有一个保存按钮。当某些数据发生更改(=HasICheckChanged)并且输入所有必需的数据(=IsAvailable)时,此按钮被启用。 调用updateSaveButtonIsEnabled () HasICheckChanged和IsAvailable是CheckedWindow的属性。如果它们发生变化,CheckedWindow调用OnICheckChanged()或OnIsAvailableChanged(),需要覆盖它们以更新Save按钮状态。 封面下面会发生什么 所使用的控件需要提供ICHeck接口的功能。WpfWindowsLib提供了以下控件: 复选框combobox datepicker decimaltextbox电子邮件文本框integertextbox phonetextbox文本框 一个不局限于这些控件,但可以继承任何现有的控件和添加一个IChecker,它实现了ICheck接口功能: 隐藏,收缩,复制Code

namespace WpfWindowsLib {

  public interface ICheck {

    /// <summary>
    /// Has the user changed the initial value of the control ?
    /// </summary>
    bool HasChanged { get; }

    /// <summary>
    /// Needs the user to change the initial value of the control ?
    /// </summary>
    bool IsRequired { get; }

    /// <summary>
    /// Has the user changed the initial value of the required control ?
    /// </summary>
    bool IsAvailable { get; }

    /// <summary>
    /// Raised when the user changes the initial value of the control 
    /// or when the user undoes any change and
    /// enters the initial value again.
    /// </summary>
    event Action  HasChangedEvent;

    /// <summary>
    /// Raised when the user changes the initial value of the required value control 
    /// or when the user undoes 
    /// any change and enters the initial value again.
    /// </summary>
    event Action  IsAvailableEvent;

    /// <summary>
    /// Tells the control to use the present value as initial value.
    /// </summary>
    void ResetHasChanged();

    /// <summary>
    /// Changes the background color of the control if its value is now 
    /// different than the initial value 
    /// and isChanged is true. If isChanged is false, the background color 
    /// gets displayed from when the
    /// control got initialised.
    /// </summary>
    /// <paramname="isChanged"></param>
    void ShowChanged(bool isChanged);
  }
}

在初始化期间,ICheck控件搜索它所在的窗口。如果该窗口继承自CheckedWindow,它就会注册到该窗口。在注册期间,CheckedWindow订阅控件的HasChangedEvent和IsAvailableEvent事件。当用户在控件中更改一些数据并导致HasChanged或IsAvailable的更改时,该控件会引发适当的事件,向CheckedWindow发出警报。CheckedWindow查询所有已注册的控件,以评估其自身的HasICheckChanged或IsAvailable属性是否需要更改,然后调用OnICheckChanged和OnIsAvailableChanged(这给继承窗口提供了机会)相应地启用或禁用Save按钮。当用户试图关闭窗口时,CheckedWindow检查是否有任何控件没有保存数据。如果是,它将标记这些控件,以便用户可以看到尚未保存的内容。然后CheckedWindow询问用户是否真的想关闭窗口并丢失输入的数据。 得到WpfWindowsLib 最新版本可以从Github下载:https://github.com/PeterHuberSg/WpfWindowsLib。 下载或克隆一切到你的PC,这给你一个解决方案WpfWindowsLib与以下项目: WpfWindowsLib:(.Dll)引用您的其他解决方案示例:显示所有WpfWindowsLib控件的wpfwindowslibtest WpfWindowsLib单元测试WpfWindowsLib核心应用程序 推荐阅读 电子邮件地址验证详细说明(WpfWindowsLib)国际电话号码验证详细说明(WpfWindowsLib)使用绑定的WPF数据格式化指南 历史 2020年2月20日:初版 本文转载于:http://www.diyabc.com/frontweb/news2572.html

posted @ 2020-08-08 15:47  Dincat  阅读(427)  评论(0编辑  收藏  举报