2

On a WPF application, i use binding on severals TextBox with ExceptionValidationRule. I would like to be able to stay on the contrôls which have an ApplicationException whitout leaving it until it have a correct data input. Is there possible ?

Here is my code :

 public string strEvtType
    {
        get { return m_strEvtType; }
        set {
                m_strEvtType = value;
                if (m_objEvtCode.ReadEvtTypebyType(m_strEvtType) != 0)
                {
                    throw new ApplicationException("Error ! : " + m_strEvtType.Trim() + " don't exist");
                }
                FirePropertyChangedEvent("strEvtType");
                FirePropertyChangedEvent("m_objEvtCode.strDes");

            }

My xaml :

<TextBox Name="TextBox_TypeEvenement" Grid.Column="1" VerticalAlignment="Center" Height="20" LostFocus="TextBox_TypeEvenement_LostFocus">
  <TextBox.Text>
    <Binding Path="strEvtType">
        <Binding.ValidationRules>
            <ExceptionValidationRule />
        </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
</TextBox>

My Template

<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
        <ControlTemplate>
            <DockPanel  LastChildFill="False">
                <TextBlock DockPanel.Dock="Right"
                Foreground="red"
                FontSize="9pt"
                Text="{Binding ElementName=MyAdorner,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                </TextBlock>
                <Border BorderBrush="Red" BorderThickness="1">
                    <AdornedElementPlaceholder Name="MyAdorner" />
                </Border>
            </DockPanel>
        </ControlTemplate>
    </Setter.Value>
</Setter>

Thanks a lot :)

Best regards,

Nixeus

Walter Fabio Simoni
  • 5,671
  • 15
  • 55
  • 80

0 Answers0