2011-10-03 56 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的網格的不透明度和前景色,而不是樣式本身。

希望這會有所幫助。