Note that there are some explanatory texts on larger screens.

plurals
  1. POInitial validation on dynamically added control
    primarykey
    data
    text
    <p>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.</p> <p>Suppose we have MyObject class</p> <pre><code> 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. */ } </code></pre> <p>And some user control, say MyUserControl, that allows editing of MyObject objects. It can look somehow like this:</p> <pre><code>&lt;UserControl x:Class="Test.MyUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="Name: "/&gt; &lt;TextBox Text="{Binding Name, ValidatesOnDataErrors=True}" Width="200"/&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre> <p>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.)</p> <p>Suppose that application window consist of a dockpanel (dockPanel) and a button:</p> <pre><code>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(); } </code></pre> <p>Why?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload