如何通过 tabcontrol tabitem 选择更改重新更新文本框验证

沙斯特

在尝试将我的实体保存到数据库之前,我必须验证一些属性。

问题:

  1. 程序启动:tabitem1 中的文本框显示验证错误
  2. 用户选择:视图中的 Tabitem2
  3. 用户选择:视图中的 Tabitem1
  4. tabitem1 中不再显示空文本框的验证错误。

异常行为:

如果用户更改了选定的选项卡,则每次也应显示验证错误。

使用的工具/框架:

  1. Prism 6.3(带有模板包 PrismUnity 的新项目
  2. Prism.Validation ( https://github.com/mfe-/Prism.Validation )

问题:

  1. 为什么在不同选项卡之间选择后不再显示 DataAnnoation?ViewModel 属性 hasErrors 为 true。
  2. 如果用户再次选择 tabitem1,我如何重新开始评估?

看法:

<Window x:Class="PrismUnityApp1TestValidation.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525">
    <StackPanel>
        <!--<ContentControl prism:RegionManager.RegionName="ContentRegion" />-->
        <TabControl>
        <TabItem>
         <TabItem.Content>
             <TextBox Height="50" Text="{Binding TestText, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></TextBox>
         </TabItem.Content>
        </TabItem>
            <TabItem>
                <TabItem.Content>
                   <TextBlock Text="TabItem2"></TextBlock>
                </TabItem.Content>
            </TabItem>

        </TabControl>
    </StackPanel>
</Window>

视图模型:

using System.ComponentModel.DataAnnotations;
using Prism.Mvvm;
using Prism.Validation;

namespace PrismUnityApp1TestValidation.ViewModels
{
    public class MainWindowViewModel : ValidatableBindableBase
    {
        private string _title = "Prism Unity Application";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private string _testtext;
        [Required]
        public string TestText
        {
            get { return _testtext; }
            set { SetProperty(ref _testtext, value); }
        }


        public MainWindowViewModel()
        {

        }
    }
}

ValidatableBindableBase (NugetPackage Prism.Validation):

namespace Prism.Validation
{
    /// <summary>
    /// The IValidatableBindableBase interface was created to add validation support for model classes that contain validation rules.
    /// The default implementation of IValidatableBindableBase is the ValidatableBindableBase class, which contains the logic to run the validation rules of the
    /// instance of a model class and return the results of this validation as a list of properties' errors.
    /// </summary>
    // Documentation on validating user input is at http://go.microsoft.com/fwlink/?LinkID=288817&clcid=0x409
    public class ValidatableBindableBase : BindableBase, IValidatableBindableBase, INotifyDataErrorInfo
    {
        private readonly BindableValidator _bindableValidator;

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidatableBindableBase"/> class.
        /// </summary>
        public ValidatableBindableBase()
        {
            _bindableValidator = new BindableValidator(this);
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is validation enabled.
        /// </summary>
        /// <value>
        /// <c>true</c> if validation is enabled for this instance; otherwise, <c>false</c>.
        /// </value>
        public bool IsValidationEnabled
        {
            get { return _bindableValidator.IsValidationEnabled; }
            set { _bindableValidator.IsValidationEnabled = value; }
        }

        /// <summary>
        /// Returns the BindableValidator instance that has an indexer property.
        /// </summary>
        /// <value>
        /// The Bindable Validator Indexer property.
        /// </value>
        public BindableValidator Errors
        {
            get
            {
                return _bindableValidator;
            }
        }
        /// <summary>
        /// Gets a value that indicates whether the entity has validation errors.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance contains validation errors; otherwise, <c>false</c>.
        /// </value>
        public bool HasErrors
        {
            get
            {
                return !ValidateProperties();
            }
        }

        /// <summary>
        /// Occurs when the Errors collection changed because new errors were added or old errors were fixed.
        /// </summary>
        public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged
        {
            add { _bindableValidator.ErrorsChanged += value; }

            remove { _bindableValidator.ErrorsChanged -= value; }
        }

        /// <summary>
        /// Gets all errors.
        /// </summary>
        /// <returns> A ReadOnlyDictionary that's key is a property name and the value is a ReadOnlyCollection of the error strings.</returns>
        public ReadOnlyDictionary<string, ReadOnlyCollection<string>> GetAllErrors()
        {
            return _bindableValidator.GetAllErrors();
        }

        /// <summary>
        /// Validates the properties of the current instance.
        /// </summary>
        /// <returns>
        /// Returns <c>true</c> if all properties pass the validation rules; otherwise, false.
        /// </returns>
        public bool ValidateProperties()
        {
            return _bindableValidator.ValidateProperties();
        }

        /// <summary>
        /// Validates a single property with the given name of the current instance.
        /// </summary>
        /// <param name="propertyName">The property to be validated.</param>
        /// <returns>Returns <c>true</c> if the property passes the validation rules; otherwise, false.</returns>
        public bool ValidateProperty(string propertyName)
        {
            return !_bindableValidator.IsValidationEnabled // don't fail if validation is disabled
                || _bindableValidator.ValidateProperty(propertyName);
        }

        /// <summary>
        /// Sets the error collection of this instance.
        /// </summary>
        /// <param name="entityErrors">The entity errors.</param>
        public void SetAllErrors(IDictionary<string, ReadOnlyCollection<string>> entityErrors)
        {
            _bindableValidator.SetAllErrors(entityErrors);
        }

        /// <summary>
        /// Checks if a property already matches a desired value. Sets the property and
        /// notifies listeners only when necessary. We are overriding this property to ensure that the SetProperty and the ValidateProperty methods are fired in a
        /// deterministic way.
        /// </summary>
        /// <typeparam name="T">Type of the property.</typeparam>
        /// <param name="storage">Reference to a property with both getter and setter.</param>
        /// <param name="value">Desired value for the property.</param>
        /// <param name="propertyName">Name of the property used to notify listeners. This
        /// value is optional and can be provided automatically when invoked from compilers that
        /// support CallerMemberName.</param>
        /// <returns>
        /// True if the value was changed, false if the existing value matched the
        /// desired value.
        /// </returns>
        protected override bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            var result = base.SetProperty(ref storage, value, propertyName);

            if (result && !string.IsNullOrEmpty(propertyName))
            {
                if (_bindableValidator.IsValidationEnabled)
                {
                    _bindableValidator.ValidateProperty(propertyName);
                }
            }
            return result;
        }
        /// <summary>
        /// Gets the validation errors for a specified property or for the entire entity.
        /// </summary>
        /// <param name="propertyName">The name of the property to retrieve validation errors for; or null or Empty, to retrieve entity-level errors.</param>
        /// <returns>The validation errors for the property or entity.</returns>
        public IEnumerable GetErrors(string propertyName)
        {
            if (HasErrors==false)
            {
                return Enumerable.Empty<String>();
            }
            return _bindableValidator[propertyName];
        }
    }
}
沙斯特

我在这里找到了解决方案:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/fb50537c-feec-42dc-8439-dcf78ef8951a/validation-error-and-tab-control?forum=wpf

我在帖子中提到了我的观点。

看法:

<Window x:Class="PrismUnityApp1TestValidation.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525">
    <StackPanel>
        <!--<ContentControl prism:RegionManager.RegionName="ContentRegion" />-->
        <TabControl>
        <TabItem IsSelected="{Binding TabItem1Selected}">
         <TabItem.Content>
             <AdornerDecorator>
                 <TextBox Height="50" Text="{Binding TestText, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></TextBox>
                    </AdornerDecorator>

         </TabItem.Content>
        </TabItem>
            <TabItem>
                <TabItem.Content>
                   <TextBlock Text="TabItem2"></TextBlock>
                </TabItem.Content>
            </TabItem>

        </TabControl>
    </StackPanel>
</Window>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何获取TabControl WPF的TabItem标头的宽度

来自分类Dev

如何设置TabControl的TabItem的样式而不设置其ContentPresenters的样式?

来自分类Dev

WPF - 清除 TabControl 的所有 TabItem 中的所有文本框

来自分类Dev

如何通过MouseOver更改TabItem的页眉图像?

来自分类Dev

如何更改TabItem的光标

来自分类Dev

如何将第一个TabItem与其TabControl的左边框对齐?

来自分类Dev

如何在WPF中强制内容卸载我的TabControl的所有TabItem?(使用Caliburn.micro)

来自分类Dev

WPF TabControl,使用C#代码更改TabItem的背景色

来自分类Dev

Tabcontrol TabItem中的WPF页面延迟加载

来自分类Dev

WPF TabControl覆盖TabItem背景吗?

来自分类Dev

单击 TabControl 的当前 tabItem(在 WPF 中)

来自分类Dev

将 TabItem 拉伸到 TabControl 宽度

来自分类Dev

更改 tabcontrol WPF 上的选项卡时,从另一个窗口重新加载 TabItem 中的数据

来自分类Dev

通过代码更改TabControl的选项卡

来自分类Dev

WPF:TabControl.ItemTemplate和TabItem.ContentTemplate之间的区别

来自分类Dev

Windows Classic主题上的WPF Tabcontrol TabItem显示问题

来自分类Dev

在动态填充的TabControl中为TabItem设置正确的DataContext

来自分类Dev

WPF- TabControl,每个TabItem中都有Datagrid

来自分类Dev

替换加载在TabControl TabItem中的UserControl的最佳方法

来自分类Dev

在动态填充的TabControl中为TabItem设置正确的DataContext

来自分类Dev

具有可关闭TabItem标头的TabControl

来自分类Dev

''TabItem' ControlTemplate TargetType 与模板化类型 'TabControl' 不匹配。

来自分类Dev

如何更改TabControl选定的选项卡

来自分类Dev

TabControl:所有TabItem均已折叠,但第一个TabItem的内容仍然可见

来自分类Dev

如何通过使用XAML中的触发器来更改Tabitem的背景?

来自分类Dev

如何将TabItem的标题与TabItem DataTemplate中的文本绑定为标题?

来自分类Dev

如何清除嵌套在 tabControl、tabPage 和 2 个面板中的文本框?

来自分类Dev

如何通过TabControl模板控制Textblock属性

来自分类Dev

如何通过TabControl模板控制Textblock属性

Related 相关文章

  1. 1

    如何获取TabControl WPF的TabItem标头的宽度

  2. 2

    如何设置TabControl的TabItem的样式而不设置其ContentPresenters的样式?

  3. 3

    WPF - 清除 TabControl 的所有 TabItem 中的所有文本框

  4. 4

    如何通过MouseOver更改TabItem的页眉图像?

  5. 5

    如何更改TabItem的光标

  6. 6

    如何将第一个TabItem与其TabControl的左边框对齐?

  7. 7

    如何在WPF中强制内容卸载我的TabControl的所有TabItem?(使用Caliburn.micro)

  8. 8

    WPF TabControl,使用C#代码更改TabItem的背景色

  9. 9

    Tabcontrol TabItem中的WPF页面延迟加载

  10. 10

    WPF TabControl覆盖TabItem背景吗?

  11. 11

    单击 TabControl 的当前 tabItem(在 WPF 中)

  12. 12

    将 TabItem 拉伸到 TabControl 宽度

  13. 13

    更改 tabcontrol WPF 上的选项卡时,从另一个窗口重新加载 TabItem 中的数据

  14. 14

    通过代码更改TabControl的选项卡

  15. 15

    WPF:TabControl.ItemTemplate和TabItem.ContentTemplate之间的区别

  16. 16

    Windows Classic主题上的WPF Tabcontrol TabItem显示问题

  17. 17

    在动态填充的TabControl中为TabItem设置正确的DataContext

  18. 18

    WPF- TabControl,每个TabItem中都有Datagrid

  19. 19

    替换加载在TabControl TabItem中的UserControl的最佳方法

  20. 20

    在动态填充的TabControl中为TabItem设置正确的DataContext

  21. 21

    具有可关闭TabItem标头的TabControl

  22. 22

    ''TabItem' ControlTemplate TargetType 与模板化类型 'TabControl' 不匹配。

  23. 23

    如何更改TabControl选定的选项卡

  24. 24

    TabControl:所有TabItem均已折叠,但第一个TabItem的内容仍然可见

  25. 25

    如何通过使用XAML中的触发器来更改Tabitem的背景?

  26. 26

    如何将TabItem的标题与TabItem DataTemplate中的文本绑定为标题?

  27. 27

    如何清除嵌套在 tabControl、tabPage 和 2 个面板中的文本框?

  28. 28

    如何通过TabControl模板控制Textblock属性

  29. 29

    如何通过TabControl模板控制Textblock属性

热门标签

归档