2017-02-05 15 views
3

CheckBoxStyle을 적용하려고합니다. 문제는 다음과 같습니다. Style은 디자인 타임에 적용되지만 런타임에는 적용되지 않습니다. MainWindow.xaml에 Style을 넣을 때만 작동합니다.런타임에 WPF 스타일이 적용되지 않습니다.

리소스 사전에 Style이 있습니다. 여기가 App.xaml에서, :

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

UPDATE : 나는 Style 것을 발견 이 런에 적용되지

<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> 

이 나는 ​​스타일을 적용하려고하는 방법이다 내 시작 위치가 App이 아닌, 내가 만든 Program.cs 클래스.

+0

나에게도 좋으며 런타임에는 다음과 같습니다. 정확하게 적용되지 않는 것은 무엇입니까? –

+0

정확한 XAML은 샘플 응용 프로그램을 통과 할 때 디자인 타임과 런타임 모두에서 완벽하게 작동합니다. 런타임시에'ToggleButton' 스타일을 오버라이드 할 수있는 다른 것을 응용 프로그램에서 찾을 것입니다. – Stewbob

+0

MainWindow.xaml에 스타일을 넣을 때만 작동합니다. – user2412672

답변

1

런타임에 적용되지 않지만, 클래스 I는 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 클래스에서 파생되지 않은 클래스에서 사용하고 있다면 System.Windows 네임 스페이스에서 Style이 파생 된 것처럼 작동하지 않을 것이다. 이 코드를 사용하는 곳에서 전체 코드를 붙여 넣을 수 있습니까? – AlphaWarrior