我写了RadioButton样式 - 代码崩溃了,我没有找到任何理由让这次崩溃。
<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="RadioButtonStyle2" Storyboard.TargetProperty="Opacity" To="1"/>
<ColorAnimation Duration="0" Storyboard.TargetName="RadioButtonStyle2" Storyboard.TargetProperty="Foreground" To="Black" />
</Storyboard>
</vsm:VisualState>
答案 0 :(得分:1)
注意:此答案中的第一个代码块会尝试重复此问题:
<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>
如果所显示的代码全部存在,则会遗漏很多。
我粘贴了我用来测试你的风格的代码并添加了一些代码。看一下设计器中的异常,它可能看起来像这样:
这应该告诉你出了什么问题。在我的代码中,Foreground属性(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>
答案 1 :(得分:0)
真正的问题是
Storyboard.TargetName="RadioButtonStyle2"
RadioButtonStyle2 是此Style的名称。 TargetName应该是您在ControlTemplate中定义的控件之一的名称。
当您尝试使用此样式检查RadioButton时,它会给您一个错误。如果您使用Expression Blend,转到此样式中的Checked状态,它会向您显示错误, 动画正在尝试修改名为“RadioButtonStyle2”的对象,但在网格中找不到此类对象。
我看到你正在尝试在选中此RadioButton时更改不透明度和前景色。为此,您需要在Grid中添加ContentControl。并在Checked可视状态中更改此GridControl的网格不透明度和前景颜色,而不是样式本身。
希望这有帮助。