Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF绑定验证未显示错误消息_C#_.net_Wpf - Fatal编程技术网

C# WPF绑定验证未显示错误消息

C# WPF绑定验证未显示错误消息,c#,.net,wpf,C#,.net,Wpf,我在尝试验证用户名文本框字段时遇到问题。当我失去焦点时,我除了向验证器显示错误外 我创建了UsernameValidator类,其中ImpectValidationRule public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { if (this.username == null)

我在尝试验证用户名文本框字段时遇到问题。当我失去焦点时,我除了向验证器显示错误外

我创建了UsernameValidator类,其中Impect
ValidationRule

  public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {

            if (this.username == null)
                return new ValidationResult(false, "value cannot be empty.");
            else
            {
                if (this.username.ToString().Length > 3)
                    return new ValidationResult
                    (false, "Name cannot be more than 3 characters long.");
            }
            return ValidationResult.ValidResult;
        }
在xaml中,我设置了控件模板

<ControlTemplate x:Key="validationErrorTemplate">
    <DockPanel>
    <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
        <Grid Width="12" Height="12">
            <Ellipse Width="12" Height="12" 
            Fill="Red" HorizontalAlignment="Center" 
            VerticalAlignment="Center"

                     ></Ellipse>
            <TextBlock Foreground="White" FontWeight="Heavy" 
            FontSize="8" HorizontalAlignment="Center" 
            VerticalAlignment="Center" TextAlignment="Center"
                       ToolTip="{Binding ElementName=ErrorAdorner, 
                       Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                       >X</TextBlock>
        </Grid>
        <TextBlock Foreground="Red" FontWeight="12" Margin="2,0,0,0" 
                   Text="{Binding ElementName=ErrorAdorner, 
                   Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                   ></TextBlock>                    
        </StackPanel>
        <AdornedElementPlaceholder 
        x:Name="ErrorAdorner" ></AdornedElementPlaceholder>
    </DockPanel>
</ControlTemplate>

X

WPF by design会以静默方式使绑定操作失败(如中所示,在正常操作期间,不会向用户显示任何内容)


但是,当您在调试器下运行WPF程序(如Visual Studio中的“运行+调试”按钮)时,您将看到输出窗口中显示的绑定错误。

WPF by design会以静默方式使绑定操作失败(如中所示,在正常操作期间不会向用户显示任何内容)


但是,当您在调试器下运行WPF程序(如Visual Studio中的“运行+调试”按钮)时,您将看到输出窗口中显示的绑定错误。

我尝试编译(发布)并尝试运行。问题出在一些人身上。没有错误显示。在调试中,我检查了输出控制台,什么都没有。Snoop实用程序对于调试绑定错误非常方便。是的,我尝试了。但当我发布应用程序和手动执行app.exe时,不会显示whay消息。我尝试编译(发布)并尝试运行。问题出在一些人身上。没有错误显示。在调试中,我检查了输出控制台,什么都没有。Snoop实用程序对于调试绑定错误非常方便。是的,我尝试了。但当我发布应用程序和手动执行app.exe时,不会显示whay消息
<TextBox x:Name="usernameTextBox" Validation.ErrorTemplate="{StaticResource validationErrorTemplate}" HorizontalAlignment="Left" Height="23" Margin="115,31,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="277" PreviewKeyDown="OnUsernameTextBoxKeyDown">
<TextBox.Text>
                <Binding Path="Username" Mode="TwoWay" UpdateSourceTrigger="LostFocus">
                    <Binding.ValidationRules>
                        <local:UsernameValidator></local:UsernameValidator>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
</TextBox