【问题标题】:WPF style not applied at runtime运行时未应用 WPF 样式
【发布时间】:2020-02-19 03:35:47
【问题描述】:

我正在尝试在 CheckBox 上应用 Style。问题是:Style 在设计时应用,而不是在运行时应用。仅当我将 Style 放入 MainWindow.xaml 时它才有效。

我在资源字典中有一个Style。在这里,在 App.xaml 中:

<Application.Resources>
  <ResourceDictionary>
    <Style x:Key="ToggleButton" TargetType="{x:Type CheckBox}">
      <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
      <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type CheckBox}">
            <ControlTemplate.Resources>
              <Storyboard x:Key="OnChecking">
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="slider" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
                  <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="25"/>
                </DoubleAnimationUsingKeyFrames>
              </Storyboard>
              <Storyboard x:Key="OnUnchecking">
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="slider" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
                  <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>
                </DoubleAnimationUsingKeyFrames>
                <ThicknessAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="slider" Storyboard.TargetProperty="(FrameworkElement.Margin)">
                  <SplineThicknessKeyFrame KeyTime="00:00:00.3000000" Value="1,1,1,1"/>
                </ThicknessAnimationUsingKeyFrames>
              </Storyboard>
            </ControlTemplate.Resources>

            <DockPanel x:Name="dockPanel">
              <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" ContentTemplate="{TemplateBinding ContentTemplate}" RecognizesAccessKey="True" VerticalAlignment="Center"/>
              <Grid Margin="5,5,0,5" Width="50" Background="#FFC0CCD9">
                <TextBlock Text="ON" TextWrapping="Wrap" FontWeight="Bold" FontSize="12" HorizontalAlignment="Right" Margin="0,0,3,0"/>
                <TextBlock HorizontalAlignment="Left" Margin="2,0,0,0" FontSize="12" FontWeight="Bold" Text="OFF" TextWrapping="Wrap"/>
                <Border HorizontalAlignment="Left" x:Name="slider" Width="23" BorderThickness="1,1,1,1" CornerRadius="3,3,3,3" RenderTransformOrigin="0.5,0.5" Margin="1,1,1,1">
                  <Border.RenderTransform>
                    <TransformGroup>
                      <ScaleTransform ScaleX="1" ScaleY="1"/>
                      <SkewTransform AngleX="0" AngleY="0"/>
                      <RotateTransform Angle="0"/>
                      <TranslateTransform X="0" Y="0"/>
                    </TransformGroup>
                  </Border.RenderTransform>
                  <Border.BorderBrush>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                      <GradientStop Color="#FFFFFFFF" Offset="0"/>
                      <GradientStop Color="#FF4490FF" Offset="1"/>
                    </LinearGradientBrush>
                  </Border.BorderBrush>
                  <Border.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                      <GradientStop Color="#FF8AB4FF" Offset="1"/>
                      <GradientStop Color="#FFD1E2FF" Offset="0"/>
                    </LinearGradientBrush>
                  </Border.Background>
                </Border>
              </Grid>
            </DockPanel>

            <ControlTemplate.Triggers>
              <Trigger Property="IsChecked" Value="True">
                <Trigger.ExitActions>
                  <BeginStoryboard Storyboard="{StaticResource OnUnchecking}" x:Name="OnUnchecking_BeginStoryboard"/>
                </Trigger.ExitActions>
                <Trigger.EnterActions>
                  <BeginStoryboard Storyboard="{StaticResource OnChecking}" x:Name="OnChecking_BeginStoryboard"/>
                </Trigger.EnterActions>
              </Trigger>
              <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </ResourceDictionary>
</Application.Resources>

这就是我尝试应用样式的方式:

<CheckBox HorizontalAlignment="Center" Style="{DynamicResource ToggleButton}" VerticalAlignment="Center" Content="Test"/>

更新: 当我的启动位置不是 App,而是我创建的类 Program.cs 时,我发现 Style 没有在运行时应用。

【问题讨论】:

  • 对我来说它看起来很好,在运行时也是如此:究竟什么没有应用?
  • 当我将它粘贴到示例应用程序中时,您的确切 XAML 在设计时和运行时都能完美运行。我会在您的应用程序中寻找可能在运行时覆盖您的 ToggleButton 样式的其他内容。
  • 只有在我将样式放入 MainWindow.xaml 时它才有效

标签: c# wpf xaml wpf-style


【解决方案1】:

我发现当我的启动位置不是 App,而是我创建的类 Program.cs 时,样式在运行时没有应用。

【讨论】:

    【解决方案2】:

    为您的复选框命名并尝试动态应用样式。

    <CheckBox x:Name="chckbx1" HorizontalAlignment="Center" Style="{DynamicResource ToggleButton}" VerticalAlignment="Center" Content="Test"/>
    

    在后面的代码中:

    chckbx1.Style = (Style) FindResource("ToggleButton");
    

    【讨论】:

    • 我遇到了无法找到样式的异常
    • @user2412672 我自己试过了,效果很好。我不知道你是如何使用它的,也不知道你在哪里使用它。如果你在一个不是从 Window 类派生的类中使用它(通常),那么它将不起作用,因为 Style 派生自 System.Windows 命名空间.你能把整个代码粘贴到你正在使用的地方吗?
    【解决方案3】:

    我已经看到这种情况发生当样式TargetType 用于基类而不是特定的派生类时。在这种情况下,设计师向我展示了按预期应用的样式,但在运行时却没有。

    我特别在使用 VS2015 设计器。

    我最终按照Applying a style to all derived classes in WPFHow to make a WPF style inheritable to derived classes? 中的建议解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-09
      • 2018-11-27
      相关资源
      最近更新 更多