注:在此回答一個代碼塊試圖重複這個問題:
<UserControl x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>
<Style x:Key="RadioButtonStyle2"
TargetType="RadioButton">
<Setter Property="Foreground"
Value="#5DFFC8" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RadioButton">
<Grid>
<vsm:VisualStateManager.VisualStateGroups>
<vsm:VisualStateGroup x:Name="CheckStates">
<vsm:VisualState x:Name="Checked">
<Storyboard>
<DoubleAnimation Duration="0"
Storyboard.TargetName="RadioButtonPart"
Storyboard.TargetProperty="Opacity"
To="1" />
<ColorAnimation Duration="0"
Storyboard.TargetName="RadioButtonPart"
Storyboard.TargetProperty="Foreground"
To="Black" />
</Storyboard>
</vsm:VisualState>
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>
<RadioButton x:Name="RadioButtonPart" Foreground="{TemplateBinding Foreground}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot"
Background="White">
<RadioButton Style="{StaticResource RadioButtonStyle2}" IsChecked="True">Test</RadioButton>
</Grid>
</UserControl>
如果顯示的代碼就是一切,有很多缺失。
我粘貼了我用來測試你的風格的代碼,並添加了一些。看一看在設計之外,它可能會是這樣的:
這應該告訴你什麼是錯的。在我的代碼中,前景屬性(Brush)和動畫(Color)的類型不匹配。
您將可以使他們通過動畫刷的顏色屬性(SolidBrush)
下面是一個更好的工作樣品不一致(還沒有完成,但動畫工作)
<UserControl x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>
<Style x:Key="RadioButtonStyle2"
TargetType="RadioButton">
<Setter Property="Foreground"
Value="#5DFFC8" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RadioButton">
<Grid>
<vsm:VisualStateManager.VisualStateGroups>
<vsm:VisualStateGroup x:Name="CheckStates">
<vsm:VisualState x:Name="Checked">
<Storyboard>
<DoubleAnimation Duration="0"
Storyboard.TargetName="RadioButtonPart"
Storyboard.TargetProperty="Opacity"
To="1" />
<ColorAnimation Duration="0"
Storyboard.TargetName="radioButtonForegroundColor"
Storyboard.TargetProperty="Color"
To="Black" />
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="Unchecked">
<Storyboard>
<DoubleAnimation Duration="0"
Storyboard.TargetName="RadioButtonPart"
Storyboard.TargetProperty="Opacity"
To="0.5" />
<ColorAnimation Duration="0"
Storyboard.TargetName="radioButtonForegroundColor"
Storyboard.TargetProperty="Color"
To="Red" />
</Storyboard>
</vsm:VisualState>
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>
<RadioButton x:Name="RadioButtonPart"
IsChecked="{TemplateBinding IsChecked}"
Content="{TemplateBinding Content}">
<RadioButton.Foreground>
<SolidColorBrush Color="White"
x:Name="radioButtonForegroundColor" />
</RadioButton.Foreground>
</RadioButton>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot"
Background="White">
<StackPanel>
<RadioButton Style="{StaticResource RadioButtonStyle2}"
Content="Test 1" />
<RadioButton Style="{StaticResource RadioButtonStyle2}"
IsChecked="False"
Content="Test 2" />
</StackPanel>
</Grid>
</UserControl>
這只是風格的一部分。發佈一切,請儘可能具體:什麼崩潰?什麼是例外?你確定它是模板嗎? –
這是造成墜毀的部分。當我說這部分全部沒有崩潰時工作 – Yanshof
當你從你的解決方案中刪除這段代碼時,它不會編譯。請張貼所有樣式代碼。 –