动态添加控件的初始验证 [英] Initial validation on dynamically added control

查看:87
本文介绍了动态添加控件的初始验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF验证系统执行对象的初始验证(我的意思是-当更改数据绑定项时,所有字段均得到验证,并且结果显示在ui上)。
但是当我动态添加控件时,它不能像这样工作。在这种情况下,会进行初始验证,但不会在用户界面上显示结果。只有在数据绑定对象的某些属性发生更改后,所有内容才能开始正常工作。这是一个粗略的示例。

WPF validation system performs intial validatation of an object (I mean - all fields are validated when a databound item is changed, and results are displayed on the ui). But it doesn't work like this, when I add a control dynamically. In such case inital validation happens, but results aren't shown on the ui. Only after some properties on a databound object change, everything starts working correctly. Here's a crude sample.

假设我们有MyObject类

Suppose we have MyObject class

 public class MyObject : INotifyPropertyChanged, IDataErrorInfo
 {
    public string Name { /*get, set implementation */}        

    // IDataErrorInfo
    public string this[string columnName]
    {
        get
        {
            if (String.IsNullOrEmpty(Name)) return "Name can't be empty!";
            return String.Empty;
        }
    }
    /* etc. */
}

还有一些用户控件,例如MyUserControl,它允许编辑MyObject对象。
看起来可能像这样:

And some user control, say MyUserControl, that allows editing of MyObject objects. It can look somehow like this:

<UserControl x:Class="Test.MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Name: "/>
            <TextBox Text="{Binding Name, ValidatesOnDataErrors=True}" Width="200"/>
        </StackPanel>
</UserControl>

现在,将此控件添加到xaml的主窗口中(或在构造函数或窗口加载事件),而不是将MyCustomControl.DataContext设置为MyObject类的新实例时,将立即验证名称字段,并使用验证错误模板显示错误通知。但是,当动态添加MyCustomControl(例如,单击按钮后)时,会进行初始验证,但ui不显示结果(无红色边框等)。

Now, when this control is added to the main window in xaml (or in code behind in constructor or window loaded event) than when MyCustomControl.DataContext is set to a new instance of the MyObject class, the Name field is validated immediately and error notification is displayed using validation error template. But when MyCustomControl is added dynamically (after, say, button is clicked), initial validation happens but ui doesn't show results (no red border etc.)

假设应用程序窗口由一个停靠面板(dockPanel)和一个按钮组成:

Suppose that application window consist of a dockpanel (dockPanel) and a button:

public Window1()
        {
            InitializeComponent();

            button.Click +=new RoutedEventHandler(OnButtonClick);

            /*
            // in this case validation works correctly,
            // when window is shown Name textbox already got a red border etc.
            var muc = new MyUserControl();
            dockPanel.Children.Add(muc);
            muc.DataContext = new MyObject(); 
            */
        }


        private void OnButtonClick(object sender, RoutedEventArgs e)
        {
            // in this case validatation works, but no results are shown on the ui
            // only after Name property is changed (then removed again) red border appears
            var muc = new MyUserControl();
            dockPanel.Children.Add(muc);
            muc.DataContext = new MyObject(); 
        }

为什么?

推荐答案

好,我找到了答案。这是装饰层的问题。
我们的WPF专家已经遇到它并提供了一些解决方案。
请参见 Karl Shifflett的帖子

Ok, I found kind of an answer. That's problem with adorner layer. Our WPF gurus already encountered it and provided some solution. See Karl Shifflett's post.

这篇关于动态添加控件的初始验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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