WPF样式在运行时未应用

4

我正在尝试在 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"/>

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


我觉得它看起来很好,在运行时也是如此:到底哪些没有应用? - user6996876
当我将您的确切XAML粘贴到示例应用程序中时,它在设计时间和运行时都能完美地运行。我会在您的应用程序中寻找其他可能在运行时覆盖您的ToggleButton样式的内容。 - Stewbob
只有在MainWindow.xaml中添加样式才能使其正常工作。 - user2412672
3个回答

0

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

0

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

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

在代码后台:

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

我遇到了一个异常,提示找不到样式。 - user2412672
@user2412672,我自己尝试过,对我来说工作得很好。我不知道你是如何使用它或在哪里使用它的。如果你将它用在一个没有从Window类(通常情况下)派生出来的类中,那么它将不起作用,因为Style派生自System.Windows命名空间。能否粘贴一下你使用这个东西的完整代码呢? - AlphaWarrior

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接