Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Validation triggered by Second Control -- works but visual cue is not seen
2 replies. Latest Post by berandor2008 on July 3, 2009.
(0)
berandor...
Member
6 points
9 Posts
06-29-2009 12:05 PM |
I have 2 controls:
ComboBox with a list of potential answers ( in this case; yes, no )
If yes is chosen then I want to 'require' an entry in the accompanying TextBox.
xaml for combo and textbox:
<ComboBox x:Name="cQuestionOptions" MinWidth="100" DisplayMemberPath="AnswerText" Margin="5,0,0,0" SelectionChanged="OnQuestionOptionSelectionChanged"/> <TextBox Grid.Column="1" x:Name="cExplainText" Text="{Binding Path=Explanation, Mode=TwoWay, NotifyOnValidationError=True,ValidatesOnExceptions=True}" VerticalAlignment="Center" Margin="5,2,10,2"/>
.cs for the property and event:
private string mExplanation; public string Explanation { get { return mExplanation; } set { if ( this.ExplanationRequired && value.Length == 0 ) { throw new ArgumentException("Explanation is required."); } mExplanation = value; } } private void OnQuestionOptionSelectionChanged(object sender, SelectionChangedEventArgs e) { // refire validation on explain text var be = cExplainText.GetBindingExpression(TextBox.TextProperty); be.UpdateSource(); }
The error I am seeing is that the Red outline and corner triangle don't display around the cExplainText control. If I click into this control they then appear. If I check Validation.GetHasError( cExplainText ) it returns true.
Is there something I can do to get the red visual cue to appear? I have tried UpdateLayout() and InvalidateArrange(), InvalidateMeasure().
If I can supply more code to make things more clear, please let me know.
Mog Lian...
All-Star
15874 points
1,541 Posts
07-03-2009 2:56 AM |
I cannot repro this problem, here is my test sample, works correctly for me
Xaml
<StackPanel> <ComboBox SelectionChanged="ComboBox_SelectionChanged"> <TextBlock Text="Yes"/> <TextBlock Text="No"/> </ComboBox> <TextBox Name="tb2" Text="{Binding Explanation,Mode=TwoWay,NotifyOnValidationError=True,ValidatesOnExceptions=True}"/> </StackPanel>
public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); DataContext = new TestData(); } private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if(((ComboBox)sender).SelectedIndex==0) ((TestData)DataContext).ExplanationRequired = true; else ((TestData)DataContext).ExplanationRequired = false; var be = tb2.GetBindingExpression(TextBox.TextProperty); be.UpdateSource(); } } public class TestData { public bool ExplanationRequired { set; get; } private string mExplanation; public string Explanation { get { return mExplanation; } set { if (this.ExplanationRequired && value.Length == 0) { throw new ArgumentException("Explanation is required."); } mExplanation = value; } } }
07-03-2009 10:22 AM |
Mog Liang your example works perfectly ( which is good ). Oddly I cannot find my original sample project on my machine and when I punched it back in it works as expected.
I have a much larger project where I am having this difficulty and I will attempt to recreate the problem in a small reproducable project.
Thanks very much for spending your time looking at this.