动态添加控件的初始验证

时间:2022-06-05 00:03:03

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.

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

Suppose we have MyObject class

假设我们有MyObject类

 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. */
}

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

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

<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>

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.)

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

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

假设应用程序窗口包含dockpanel(dockPanel)和按钮:

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(); 
        }

Why?

1 个解决方案

#1


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.

好的,我找到了答案。这是adorner层的问题。我们的WPF大师已经遇到过它并提供了一些解决方案。见Karl Shifflett的帖子。

#1


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.

好的,我找到了答案。这是adorner层的问题。我们的WPF大师已经遇到过它并提供了一些解决方案。见Karl Shifflett的帖子。