2011-10-03 62 views
0

我写了RadioButton风格 - 和代码崩溃,我没有找到任何理由让这次崩溃。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

这只是风格的一部分。发布一切,请尽可能具体:什么崩溃?什么是例外?你确定它是模板吗? –

+0

这是造成坠毁的部分。当我说这部分全部没有崩溃时工作 – Yanshof

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

如果显示的代码就是一切,有很多缺失。

我粘贴了我用来测试你的风格的代码,并添加了一些。看一看在设计之外,它可能会是这样的:

Exception information

这应该告诉你什么是错的。在我的代码中,前景属性(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> 
0

真正的问题是

Storyboard.TargetName="RadioButtonStyle2" 

RadioButtonStyle2是这种风格的名称。 TargetName应该是您在ControlTemplate中定义的控件之一的名称。

当你试图用这种风格检查RadioButton时,它会给你一个错误。如果使用Expression Blend,则转到此样式中的Checked状态,它将引发错误,说明 动画试图修改名为'RadioButtonStyle2'的对象,但在网格中找不到此类对象。

我看到你正试图改变不透明度和前景颜色,当这个RadioButton被选中。为此,您需要在Grid中添加一个ContentControl。并且改变Checked视觉状态下这个ContentControl的网格的不透明度和前景色,而不是样式本身。

希望这会有所帮助。