J
Jim Hudson
I have a little <UserControl> that gets hosted in a Canvas (so that I can
position it the way I want), constructed when the app starts, and then
displayed and hidden through appropriate settings of its Visibility property.
Inside the <UserControl>, I have a couple of <TextBox> controls, each of
which is subject to validation:
<TextBox x:Uid="DestinationTextBox"
x:Name="DestinationTextBox" Style="{StaticResource TextBoxStyle}"
Validation.ErrorTemplate="{StaticResource errorTemplate}"
MinWidth="200" >
<TextBox.Text>
<Binding x:Name="DestinationBinding"
x:Uid="DestinationBinding" Path="Rule.Destination"
NotifyOnSourceUpdated="True"
UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<pemeestinationValidationRule
x:Uid="DestinationValidationRule" x:Name="DestinationValidationRule" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
I want the error template to be displayed whenever the bound data
("Rule.Destination") is invalid: both when the <UserControl> is initially
displayed, and also whenever the user enters bad data. To make that happen,
the containing application makes the <UserControl> visible, and then calls a
method of the <UserControl> that does the following:
// Getting data-binding to work the way I want it to is NOT
easy. First, we have to
// force the XAML objects to reload themselves from the bound
data. I can cause that
// to happen by clearing and then resetting the DataContext for
the XAML objects.
this.DataContext = null;
this.DataContext = this;
// THEN, I need to force the validation rules to be fired. That
only happens
// when the binding source (which is this.Rule.xxxx) gets
updated by the data-binding.
// Since the controls now have copies of the values in
this.Rule, I'll just force
// them to copy them back by calling UpdateSource(). The
validation will happen
// along the way.
this.DestinationTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
MY PROBLEM:
EVERY time that the <UserControl> is made visible, the
BindingExpression.UpdateSource() method gets called and correctly sets the
Validation.HasErrors attached property to "true". I've verified this by
displaying that property in the UI. HOWEVER, THE FIRST TIME its displayed,
the error-template adorner is not shown. If I then change the Visibility of
the <UserControl> to Collapsed (to hide it) and then Visible (to re-display
it), then the error-template adorner appears.
What do I have to do to get the error-template adorner to appear EVERY time?
position it the way I want), constructed when the app starts, and then
displayed and hidden through appropriate settings of its Visibility property.
Inside the <UserControl>, I have a couple of <TextBox> controls, each of
which is subject to validation:
<TextBox x:Uid="DestinationTextBox"
x:Name="DestinationTextBox" Style="{StaticResource TextBoxStyle}"
Validation.ErrorTemplate="{StaticResource errorTemplate}"
MinWidth="200" >
<TextBox.Text>
<Binding x:Name="DestinationBinding"
x:Uid="DestinationBinding" Path="Rule.Destination"
NotifyOnSourceUpdated="True"
UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<pemeestinationValidationRule
x:Uid="DestinationValidationRule" x:Name="DestinationValidationRule" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
I want the error template to be displayed whenever the bound data
("Rule.Destination") is invalid: both when the <UserControl> is initially
displayed, and also whenever the user enters bad data. To make that happen,
the containing application makes the <UserControl> visible, and then calls a
method of the <UserControl> that does the following:
// Getting data-binding to work the way I want it to is NOT
easy. First, we have to
// force the XAML objects to reload themselves from the bound
data. I can cause that
// to happen by clearing and then resetting the DataContext for
the XAML objects.
this.DataContext = null;
this.DataContext = this;
// THEN, I need to force the validation rules to be fired. That
only happens
// when the binding source (which is this.Rule.xxxx) gets
updated by the data-binding.
// Since the controls now have copies of the values in
this.Rule, I'll just force
// them to copy them back by calling UpdateSource(). The
validation will happen
// along the way.
this.DestinationTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
MY PROBLEM:
EVERY time that the <UserControl> is made visible, the
BindingExpression.UpdateSource() method gets called and correctly sets the
Validation.HasErrors attached property to "true". I've verified this by
displaying that property in the UI. HOWEVER, THE FIRST TIME its displayed,
the error-template adorner is not shown. If I then change the Visibility of
the <UserControl> to Collapsed (to hide it) and then Visible (to re-display
it), then the error-template adorner appears.
What do I have to do to get the error-template adorner to appear EVERY time?