如何将 App.XAML 样式应用于当前窗口

Sim*_*ons 2 c# wpf xaml

我正在尝试应用验证错误模板并在 App.XAML 中定义样式。

<Application x:Class="MY.UI.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:BP.NES.UI"
            >
    <Application.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <Grid>
                            <Border BorderBrush="#FFCB2E2E" BorderThickness="1" Background="#11FF0000" IsHitTestVisible="False" x:Name="errorBorder"/>
                            <AdornedElementPlaceholder x:Name="placeholder" />
                            <Popup AllowsTransparency="True" HorizontalAlignment="Right" HorizontalOffset="0" VerticalOffset="0" PopupAnimation="Fade" Placement="Left" 
                                   PlacementTarget="{Binding ElementName=errorBorder}" IsOpen="{Binding ElementName=placeholder, Path=AdornedElement.IsFocused, Mode=OneWay}">
                                <StackPanel Orientation="Horizontal">
                                    <Polygon  VerticalAlignment="Center" Points="0,4 4,0 4,8" Fill="#FFCB2E2E" Stretch="Fill" Stroke="#FFCB2E2E"
                                      StrokeThickness="2" />
                                    <Border Background="#FFCB2E2E" CornerRadius="4" Padding="4">
                                        <TextBlock HorizontalAlignment="Center" Foreground="White" FontWeight="Bold" Margin="2,0,0,0"
                                                   Text="{Binding ElementName=placeholder, Path=AdornedElement.ToolTip, Mode=OneWay}" />
                                    </Border>
                                </StackPanel>
                            </Popup>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="True">
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

现在在我的主窗口中,我有以下代码:

<Window x:Class="MY.UI.View.MainWindow"
         xmlns:local="clr-namespace:MY.UI.View"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        xmlns:vm="clr-namespace:MY.UI.ViewModel"
        xmlns:rules="clr-namespace:MY.UI.Validations"
        >
         <Grid x:Name="MainGrid">
          <TextBox Grid.Row="1"
                             x:Name="CellphoneNumberTextBox"
                             Grid.Column="1"
                             VerticalAlignment="Stretch"
                             Margin="10,0,0,10"
                             IsEnabled="{Binding ElementName=PereferenceRadio,Path=IsChecked}"
                             Text="{Binding CurrentEnrolmentDetail.CellNumber,NotifyOnValidationError=True,ValidatesOnNotifyDataErrors=True,UpdateSourceTrigger=PropertyChanged}">
          </TextBox>
            </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

如果我将样式移动到 Window.Resources,它就可以正常工作,但是当我在 App.XAML 中有这个时,它不起作用。这是由于命名空间的不同吗?

Thi*_*ran 5

在 App.Xaml 中设置样式键并在文本框中使用该键。

例子:

应用程序.Xaml

<Style x:Key="HeaderStyle" TargetType="TextBlock">
        <Setter Property="Foreground" Value="Gray" />
        <Setter Property="FontSize" Value="24" />
</Style>
Run Code Online (Sandbox Code Playgroud)

窗户:

<Window x:Class="WpfTutorialSamples.Styles.ExplicitStyleSample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ExplicitStyleSample" Height="150" Width="300">

<StackPanel Margin="10">
    <TextBlock>Header 1</TextBlock>
    <TextBlock Style="{StaticResource HeaderStyle}">Header 2</TextBlock>
    <TextBlock>Header 3</TextBlock>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

或使用 BasedOn 功能覆盖 如果您没有兴趣创建样式键,请尝试此操作

应用程序.Xaml

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<!-- ... -->
</Style>
Run Code Online (Sandbox Code Playgroud)

我已经在 App.xaml 的 ResourceDictionary 中声明了我的基本样式,如果我在这样的特定窗口中覆盖它们,它通常可以工作。