Skip to main content

Microsoft Silverlight

Answered Question Validation triggered by Second Control -- works but visual cue is not seenRSS Feed

(0)

berandor2008
berandor...

Member

Member

6 points

9 Posts

Validation triggered by Second Control -- works but visual cue is not seen

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 Liang - MSFT
Mog Lian...

All-Star

All-Star

15874 points

1,541 Posts

Answered Question

Re: Validation triggered by Second Control -- works but visual cue is not seen

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>
 Code 
    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;
            }
        }
    }
 if you still encountered the problem, please provide more code, we'll try to locate the problem.

Mog Liang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

berandor2008
berandor...

Member

Member

6 points

9 Posts

Re: Re: Validation triggered by Second Control -- works but visual cue is not seen

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.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities