Initial validation of dynamically added controls

WPF validation system does internal validation of the object (I mean - all fields are validated when the data-binding element changes and the results are displayed on the ui). But it doesn't work when I add control dynamically. In this case, inital validation occurs, but the results are not displayed on the ui. Only after some properties of the object are bound to the databases, everything starts working correctly. Here's a rough example.

Suppose the 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 custom control like MyUserControl that allows you to edit MyObjects. It might look 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>

      

Now when this control is added to the main window in the xaml (or in the code residing in the constructor or event loaded by the window) than when MyCustomControl.DataContext is set to a new instance of the MyObject class, the Name field is checked immediately and an error notification is displayed using validation error template. But when MyCustomControl is added dynamically (after the button is clicked the button is clicked) the initial check is done, but the ui does not show the results (no red border, etc.).

Let's assume that the application window consists of a 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(); 
        }

      

Why?

+1


source to share


1 answer


Ok I found the answer. This problem is with adorner layer. Our WPF gurus have already run into this and have provided some solution. See Karl Schifflett's post .



+1


source







All Articles