WPF验证错误:如何在不同的TextBlock中显示,而不是工具提示 [英] WPF Validation Errors: How to show in separate TextBlock, not in ToolTip

查看:171
本文介绍了WPF验证错误:如何在不同的TextBlock中显示,而不是工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,我真的要疯了,但我不能找到一个解决方案,以显示未在工具提示中的验证错误,但在无关的文本框的用户在一个分开的文本块。

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.

我想有一个TextBlock中包含验证摘要,独立于文本框疗法用户在

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

待办事项你知道这种行为的解决方案

Do you know a solution for that behaviour?

编辑:
我目前的实现看起来莫名其妙地像:

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

推荐答案

您可以使用的BindingGroup网格,当用户提交表单验证一切。

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>



按钮点击事件提交的BindingGroup网格如下所示:

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();
}



的窗口的DataContext设置一类

The Window's DataContext is set to a class

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



验证取MandatoryValidationRule类的验证方法内发生

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!");
}

这篇关于WPF验证错误:如何在不同的TextBlock中显示,而不是工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆