面向对象三大概念
2020-02-19 17:20
采纳率: 50%
浏览 575

WPF 设置阴影边框窗体后,验证样式消失

1.默认窗体打开是这样的:

图片说明

2.点击Save出现验证:

图片说明

3.加了阴影窗体后,点击save ,验证样式不出现了。

图片说明

4.窗体代码:

<Window x:Class="MVVMLightProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MVVMLightProject"
        mc:Ignorable="d"
        xmlns:shell="clr-namespace:System.Windows.Shell;assembly=PresentationFramework"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded"   Style="{StaticResource ShadowWindow}">
    <Grid >
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <StackPanel Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"  >
                <TextBox  Style="{StaticResource TextBoxBaseValidateStyle}"  Text="{Binding RoleName, Mode=TwoWay, ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}"></TextBox>
            </StackPanel>
            <StackPanel Grid.Row="0" Grid.Column="1" VerticalAlignment="Center">
                <TextBox  Style="{StaticResource TextBoxBaseValidateStyle}" Text="{Binding Address,Mode=TwoWay,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}"></TextBox>
            </StackPanel>
            <StackPanel Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal">
                <Button Content="Save" Width="100" Click="Button_Click"></Button>
                <Button Content="Close" Width="100" Margin="10,0,0,0" Click="Button_Click_1"></Button>
            </StackPanel>
        </Grid>
    </Grid>
</Window>

  1. 阴阳窗体样式:
<Style x:Key="ShadowWindow" TargetType="{x:Type Window}">
                <Setter Property="AllowsTransparency" Value="True" />
                <Setter Property="Background" Value="White" />
                <Setter Property="ResizeMode" Value="NoResize" />
                <Setter Property="WindowStyle" Value="None" />
                <Setter Property="ShowInTaskbar" Value="False"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Window}">
                            <Grid Margin="20" Background="White">
                                <Rectangle Fill="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" RadiusX="5" RadiusY="5">
                                    <Rectangle.Effect>
                                        <DropShadowEffect BlurRadius="20" ShadowDepth="0" />
                                    </Rectangle.Effect>
                                </Rectangle>
                                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Margin}" SnapsToDevicePixels="True" CornerRadius="5">
                                    <ContentPresenter />
                                </Border>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

6.TextBox样式:

  <Style TargetType="{x:Type TextBox}" x:Key="TextBoxBaseValidateStyle" >
                <Setter Property="BorderBrush" Value="#D9D9D9" />
                <Setter Property="BorderThickness" Value="1" />
                <Setter Property="Width" Value="165" />
                <Setter Property="Height" Value="28" />
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TextBox}">
                            <Grid>

                                <Rectangle RadiusX="4" RadiusY="4" Fill="{TemplateBinding Background}" Stroke="#D9D9D9" />
                                <ScrollViewer x:Name="PART_ContentHost" HorizontalScrollBarVisibility="Hidden"
                                              VerticalScrollBarVisibility="Hidden" Padding="{TemplateBinding Padding}"
                                              Focusable="false" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="Validation.ErrorTemplate"
                    Value="{StaticResource DefaultErrorTemplate}"  />
            </Style>

7.错误样式 DefaultErrorTemplate 样式

<ControlTemplate x:Key="DefaultErrorTemplate">
                <Grid x:Name="container">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition>
                        </ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <AdornedElementPlaceholder Grid.Column="0" Name="customAdorner" VerticalAlignment="Center" />
                    <Grid Grid.Column="1" Background="White" Margin="3,0,0,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto">
                            </ColumnDefinition>
                            <ColumnDefinition>
                            </ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Border Grid.Column="0" Grid.ColumnSpan="2" Background="White">
                            <Border.Effect>
                                <DropShadowEffect Color="Black" BlurRadius="16" ShadowDepth="5" Opacity="0.2" />
                            </Border.Effect>
                        </Border>
                        <Border Grid.Column="0" x:Name="ValidationErrorElement" BorderBrush="White" BorderThickness="1" CornerRadius="1" ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                        </Border>
                        <Grid Grid.Column="1" VerticalAlignment="Center">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto">
                                </ColumnDefinition>
                                <ColumnDefinition Width="*">
                                </ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Border Grid.Column="0" Background="Red" HorizontalAlignment="Right" VerticalAlignment="Center" Width="15" Height="15" Margin="0,0,5,0" CornerRadius="10" ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                                <TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white" />
                            </Border>
                            <TextBlock Grid.Column="1" Padding="5,5" Foreground="Red" Text="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" VerticalAlignment="Center" HorizontalAlignment="Left">
                            </TextBlock>
                        </Grid>
                    </Grid>
                </Grid>

            </ControlTemplate>

大佬帮忙看一下,谢谢了。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • blownewbee 2020-02-19 18:03
    已采纳

    单独去掉

    <Rectangle Fill="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" RadiusX="5" RadiusY="5">
    

    以及去掉

    <Setter Property="AllowsTransparency" Value="True" />
    

    看看是否正常,如果正常,问题就出在这里,应该是重绘的代码覆盖了提示信息,可以用label来承载错误提示,不能用默认的了

    点赞 打赏 评论

相关推荐 更多相似问题