Silverlight 崩溃的单选按钮样式(?)
我写的是RadioButton风格的——代码崩溃了,我找不到任何原因造成这种崩溃Silverlight 崩溃的单选按钮样式(?),silverlight,Silverlight,我写的是RadioButton风格的——代码崩溃了,我找不到任何原因造成这种崩溃 <Style x:Key="RadioButtonStyle2" TargetType="RadioButton"> <Setter Property="Foreground" Value="#5DFFC8" /> <Setter Property="Template"> <Setter.Value&g
<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>
注意:此答案中的第一段代码试图重复该问题:
<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>
试验
如果显示的代码是全部代码,那么就会缺少很多代码
我在这里粘贴了用于测试您的风格的代码,并添加了一些。请查看设计器中的异常情况,它可能如下所示:
这应该告诉你出了什么问题。在我的代码中,前景属性(笔刷)和动画(颜色)的类型不匹配
您可以通过设置笔刷(SolidBrush)颜色属性的动画使它们匹配
下面是一个更好的工作示例(仍不完整,但动画工作正常)
真正的问题是
Storyboard.TargetName="RadioButtonStyle2"
RadioButtonStyle2是此样式的名称。TargetName应该是您在ControlTemplate中定义的一个控件的名称
当您尝试检查此样式的单选按钮时,它会给您一个错误。如果使用Expression Blend,请转到此样式中的选中状态,它将抛出一个错误,提示:,
动画试图修改名为“RadioButtonStyle2”的对象,但在网格中找不到此类对象
当选中此单选按钮时,我看到您正在尝试更改不透明度和前景颜色。为此,需要在网格中添加ContentControl。并在选中的视觉状态下更改网格的不透明度和此ContentControl的前景色,而不是样式本身
希望这有帮助。这只是风格的一部分。发布所有内容,请尽可能具体:什么崩溃?例外情况是什么?你确定是模板吗?这是导致崩溃的部分。当我提到这一部分时,所有工作都不会崩溃。当您从解决方案中删除此代码时,它将不会编译。请发布所有的样式代码。是的-它的编译。。。代码的其余部分只是关闭xaml样式(..\>)
Storyboard.TargetName="RadioButtonStyle2"