c# - WPF 验证错误 : How to show in separate TextBlock, 不在工具提示中

标签 c# wpf validation xaml

我认为我真的要疯了,但我找不到解决方案来显示验证错误,而不是在工具提示中,而是在与用户输入的文本框无关的单独文本 block 中。

我想要一个包含验证摘要的 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>

谢谢 - 格哈德

最佳答案

您可以使用网格的 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 类的 Validation 方法中进行

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

关于c# - WPF 验证错误 : How to show in separate TextBlock, 不在工具提示中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6753206/

相关文章:

c# - 触发 UpdatePanel 触发器后立即执行 Javascript

c# - 日期字段中具有可为空值的存储过程

javascript - Sequelize 自定义验证 - 无法访问实体中的所有字段

c# - WPF 数据绑定(bind)到接口(interface)而不是实际对象 - 可以转换吗?

WPF DataGrid 绑定(bind)到 CurrentItem 或 SelectedItem?

http - 当表单没有发布到它所在的页面时,返回错误状态的最佳方法是什么?

java - 具有附加约束的 PlayFramework 表单验证

c# - 使用数组 C# 复制文件的进度条

C# 正则表达式排序字母和数字字符串

c# - 在 XAML x :Type markup 中使用泛型类型