2017-02-05 128 views
3

我試圖在CheckBox上應用Style。問題是:Style在設計時應用,但不是在運行時應用。只有當我把Style放在MainWindow.xaml中時它才起作用。在運行時不應用WPF樣式

我在資源字典中有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"/> 

UPDATE: 我發現了Style沒有在運行 - 應用當我的啓動位置不是應用程序,而是我創建的類Program.cs。

+0

它對我來說看起來很好,在運行時也是如此:什麼不適用? –

+0

當我將它放入示例應用程序中時,您的準確XAML在設計時和運行時都可以完美運行。我會在應用程序中尋找其他可能會在運行時重寫「ToggleButton」風格的其他內容。 – Stewbob

+0

只有當我將風格放在MainWindow.xaml中時才起作用 – user2412672

回答

1

我發現這種風格在運行時沒有申請時,我的啓動位置不是應用,而是類,我創建的Program.cs。

0

爲您的複選框命名並嘗試動態應用樣式。

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

在後面的代碼:

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

我收到了風格無法找到的例外 – user2412672

+0

@ user2412672我自己嘗試過,對我來說工作得很好。我不知道你是如何使用它或你在哪裏使用它。如果你在一個不是從Window類派生的類中使用它(一般情況下),那麼它將不起作用,因爲Style派生自System.Windows名稱空間。你可以把你的整個代碼粘貼到你正在使用的地方。 – AlphaWarrior

相關問題