【问题标题】:How to apply App.XAML style to current window如何将 App.XAML 样式应用于当前窗口
【发布时间】:2017-03-27 04:53:30
【问题描述】:

我正在尝试在 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 中有这个时,它就不起作用了。这是因为命名空间的不同吗?

【问题讨论】:

标签: c# wpf xaml


【解决方案1】:

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

示例:

App.Xaml

<Style x:Key="HeaderStyle" TargetType="TextBlock">
        <Setter Property="Foreground" Value="Gray" />
        <Setter Property="FontSize" Value="24" />
</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>

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

App.Xaml

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<!-- ... -->
</Style>

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

【讨论】:

  • 如果我需要应用所有文本框,为什么我需要一个密钥?
  • 我已经更新了答案以创建覆盖样式来为您项目中的所有文本框设置。
  • @Simsons 让我知道第二种方法是否适合您。
  • 它给出了错误,异常:找不到名为“TabItemStyle”的资源。资源名称区分大小写。
【解决方案2】:

如果所有 TextBox 将保持与在资源文件中使用以下类似的思想应用程序。

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

否则在 App.xaml 中创建密钥并在需要的地方使用 TextBox,如下所示:

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

【讨论】:

  • 异常:找不到名为“TabItemStyle”的资源。资源名称区分大小写。
猜你喜欢
  • 1970-01-01
  • 2019-12-13
  • 1970-01-01
  • 2017-10-11
  • 2015-02-26
  • 1970-01-01
  • 2011-07-17
  • 1970-01-01
  • 2021-09-01
相关资源
最近更新 更多