2017-02-05 10 views
3

StyleCheckBoxに適用しようとしています。問題は、Styleがデザイン時に適用されますが、実行時には適用されません。 MainWindow.xamlにStyleを入れている場合にのみ動作します。実行時に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はランで適用されていないことが判明しましたスタートアップの場所がAppではなく、作成した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

関連する問題