开发者

WPF Validation Errors: How to show in separate TextBlock, not in ToolTip

开发者 https://www.devze.com 2023-03-21 17:53 出处:网络
I think that I\'m really going crazy, but I can\'t find a solution to show the validation Errors not in a tooltip, but in a separated textblock that has nothing to do with the TextBox the user types i

I think that I'm really going crazy, but I can't find a solution to show the validation Errors not in a tooltip, but in a separated textblock that has nothing to do with the TextBox the user types in.

I want to have one TextBlock that contains the validation summary, independent which textbox ther user types in.

D开发者_如何学JAVAo you know a solution for that behaviour?

EDIT: My Current implementation looks somehow like that:

<TextBox Margin="{StaticResource WinHorizontalMargin}" 
         Style="{StaticResource WinInputBoxNormalStyle}">
   <TextBox.Text>
      <Binding Path="AccessCode" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
         <Binding.ValidationRules>
            <ValidationRules:MandatoryValidationRule Field="Access Code"/> 
         </Binding.ValidationRules>
      </Binding>
   </TextBox.Text>
</TextBox>

<!-- Content Error Message -->
<TextBlock Grid.Row="2" Grid.ColumnSpan="2" Text="{Binding Path=(Validation.Errors)[0].ErrorContent}">
</TextBlock>

Thanks - Gerhard


You can use the BindingGroup for the grid and validate everything when user submits the form.

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:WpfApplication1="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <WpfApplication1:ErrorsToMessageConverter x:Key="e2mConverter"/>
    </Window.Resources>
    <Grid x:Name="TheGrid">

        <Grid.BindingGroup>
            <BindingGroup Name="UserInputBindingGroup">
                <BindingGroup.ValidationRules>
                    <WpfApplication1:MandatoryValidationRule/>
                </BindingGroup.ValidationRules>
            </BindingGroup>
        </Grid.BindingGroup>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBox x:Name="theTextBox">
            <TextBox.Text>
                <Binding Path="AccessCode" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">                            
                </Binding>
            </TextBox.Text>
        </TextBox>

        <!-- Content Error Message -->
        <TextBlock Grid.Row="1" Text="{Binding ElementName=TheGrid, Path=(Validation.Errors), Converter={StaticResource e2mConverter}}">
        </TextBlock>

        <Button Grid.Row="2" Content="Submit" Click="Button_Click" />
    </Grid>
</Window>

The button click event commits the BindingGroup for the grid as shown here:

private void Button_Click(object sender, RoutedEventArgs e)
{
    this.TheGrid.BindingGroup.CommitEdit();
}

The Window's DataContext is set to a class

public class UserInputValues
{
    public string AccessCode { get; set; }
}

Validation takes place within the Validation method of the MandatoryValidationRule class

public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
    BindingGroup bindingGroup = (BindingGroup) value;
    UserInputValues userInputValues = (UserInputValues) bindingGroup.Items[0];


    object accessCode = bindingGroup.GetValue(userInputValues, "AccessCode");

    // Validation code here...

    return new ValidationResult(false, "No no no!");
}


Bind the TextBlock using an ElementName binding to the TextBox that is providing notification of errors. You may also want to write a converter that converts the collection of validation errors to text so that it is easier to present in the separate TextBlock.

0

精彩评论

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