开发者

How to validate the combination of values in two different TextBoxes?

开发者 https://www.devze.com 2023-03-26 04:02 出处:网络
How can i validate them with combination Like PropertyNumber and OwnerNumber combination should not exist in database. Right now i am using IDataErrorInfo but it only validates one value at a time.

How can i validate them with combination Like PropertyNumber and OwnerNumber combination should not exist in database. Right now i am using IDataErrorInfo but it only validates one value at a time.

       <Label DockPanel.Dock="Top" Width="Auto" Height="16"/>

       <TextBox  Name="PropertyNumber" DockPanel.Dock="Top" Text="{Binding 
        UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Path=SelectedPropertyNumber, 
        ValidatesOnDataErrors=True}" Width="115"  Height="22" 
        HorizontalAlignment="Left" IsEnabled="{Binding 
  开发者_开发技巧      PropertyNumbersEnabled,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" />

       <Label DockPanel.Dock="Top" Width="Auto" Height="16"/>

       <TextBox Name="OwnerNumber" Text="{Binding BindingGroupName=NGLDataFormGrp, 
        Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, 
        Path=OwnerNumber}" DockPanel.Dock="Top"  HorizontalAlignment="Left" 
        Width="115" Height="22"  IsEnabled="{Binding 
        UpdateSourceTrigger=PropertyChanged, 
        Path=OwnerNumberEnabled, Mode=TwoWay}"/>


You'll need to use BindingGroup for that. Vincent Sibal has a great blog entry about that: BindingGroups with Item-level Validation.


I'm not sure if this is the best solution but it works for me. The way I usually handle this is create a DataTrigger binded to a boolean property which indicates valid or invalid.

If the boolean is false, I style the border red. If not, no border color.

Here's an example DataTrigger:

  <Style TargetType="{x:Type TextBox}">
     <Setter Property="TextElement.FontFamily" Value="Calibri" />
     <Setter Property="TextElement.FontSize" Value="14" />
     <Setter Property="TextElement.Foreground" Value="Black" />
     <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsValid}" Value="False">
           <Setter Property="Effect">
              <Setter.Value>
                <DropShadowEffect BlurRadius="5" Color="Red" ShadowDepth="0" />
              </Setter.Value>
           </Setter>
           <Setter Property="ToolTip" Value="Message Field entered does not exist in Message Output tree." />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=IsValid}" Value="True">
           <Setter Property="Effect" Value="{x:Null}" />
        </DataTrigger>
     </Style.Triggers>
  </Style>

If you apply this style to both of our textboxes bound to the same boolean, they'll both appear with a glow red border when the boolean is false.

0

精彩评论

暂无评论...
验证码 换一张
取 消