WPF验证错误:如何在单独的TextBlock中显示,而不是在ToolTip中显示

时间:2011-07-19 19:49:11

标签: c# wpf validation xaml

我认为我真的很疯狂,但我找不到一个解决方案来显示验证错误,而不是在工具提示中,而是在一个与用户输入的TextBox无关的独立文本块中。 / p>

我希望有一个包含验证摘要的TextBlock,独立于文本框用户输入的内容。

你知道这种行为的解决方案吗?

编辑: 我的当前实现看起来像这样:

<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>

由于 - 格哈德

2 个答案:

答案 0 :(得分:2)

您可以将BindingGroup用于网格,并在用户提交表单时验证所有内容。

<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,如下所示:

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

Window的DataContext设置为类

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

验证发生在MandatoryValidationRule类的验证方法

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

答案 1 :(得分:1)

使用ElementName绑定TextBlock绑定到提供errors通知的TextBox。您可能还想编写一个转换器,将验证错误集合转换为文本,以便更容易在单独的TextBlock中显示。