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

C# 如何将App.XAML样式应用于当前窗口,c#,wpf,xaml,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-namespac

我正在尝试应用验证错误模板并在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>

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

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


如果我将样式移动到Window.Resources,它就可以正常工作,但当我在App.XAML中使用它时,它就不起作用了。这是因为名称空间不同吗?

在App.Xaml中设置一个样式键,并使用文本框中的键

示例:

App.Xaml

<Style x:Key="HeaderStyle" TargetType="TextBlock">
        <Setter Property="Foreground" Value="Gray" />
        <Setter Property="FontSize" Value="24" />
</Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<!-- ... -->
</Style>

窗口:

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

标题1
标题2
标题3
或使用BasedOn功能覆盖 如果您对创建样式键不感兴趣,请尝试此操作

App.Xaml

<Style x:Key="HeaderStyle" TargetType="TextBlock">
        <Setter Property="Foreground" Value="Gray" />
        <Setter Property="FontSize" Value="24" />
</Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<!-- ... -->
</Style>


我已经在App.xaml中的ResourceDictionary中声明了我的基本样式,如果我在这样的特定窗口中重写它们,它通常会起作用。

如果所有文本框都将保持与资源文件中使用的以下内容相似的话

     <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
          <Setter Property="Background" Value="Blue"/>
     </Style>

否则,请在App.xaml中创建密钥,并在需要时在文本框中使用,如下所示:

 <Style x:Key="TextboxBackgroundColor" TargetType="TextBox">
      <Setter Property="Background" Value="Cyan"/>
 </Style>
 <TextBox x:Name="txtText" Style="{StaticResource TextboxBackgroundColor}" />


如果我需要应用所有文本框,请查看此链接,为什么我需要一个键?我已更新了“创建覆盖样式”的答案,以便为项目中的所有文本框设置。@Simons告诉我第二种方法是否适用于您。它给出了错误,异常:找不到名为“TabItemStyle”的资源。资源名称区分大小写。异常:找不到名为“TabItemStyle”的资源。资源名称区分大小写。