2012-04-14 43 views
1

我試圖將樣式應用於所有按鈕。當我我的風格分配給第二個按鈕,並嘗試運行我的應用程序,我得到一個錯誤:WP7將樣式應用於多個按鈕

A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.dll 
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll 
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll 

運用我的風格,以一個按鈕,將工作。

這是我的風格:

<phone:PhoneApplicationPage.Resources> 
     <Style x:Key="ButtonStyle1" TargetType="Button"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Grid Background="Transparent"> 
          <VisualStateManager.VisualStateGroups> 
           <!--Define the states for the common states. The states in a 
             VisualStateGroup are mutually exclusive to each other.--> 
           <VisualStateGroup x:Name="CommonStates"> 
            <!--Define the VisualStates in this VistualStateGroup.--> 
            <VisualState x:Name="Normal"/> 
            <VisualState x:Name="MouseOver" /> 
            <VisualState x:Name="Pressed"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ColorAnimation Duration="0" To="White" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ButtonBackground" d:IsOptimized="True"/> 
             </Storyboard>           
            </VisualState> 
            <VisualState x:Name="Disabled"> 
             <Storyboard> 
              <DoubleAnimation Duration="0" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity" To="1"/> 
             </Storyboard> 
            </VisualState> 
            </VisualStateGroup> 
           <!--Define the states for the focus states. The states in a 
             VisualStateGroup are mutually exclusive to each other.--> 
           <VisualStateGroup x:Name="FocusStates"> 
            <!--Define the VisualStates in this VistualStateGroup.--> 
            <VisualState x:Name="Focused" /> 
            <VisualState x:Name="Unfocused" /> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 
          <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}" Background="{TemplateBinding Background}"> 
           <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
          </Border> 
         </Grid> 
         <!--The parts of the button control will be defined here.--> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </phone:PhoneApplicationPage.Resources> 

這裏是我的按鈕:

<Button Height="121" HorizontalAlignment="Left" Margin="14,61,0,0" Name="but1" VerticalAlignment="Top" Width="197" Background="#FF7D0000" BorderBrush="Black" Foreground="Red" BorderThickness="0" Padding="0" Style="{StaticResource ButtonStyle1}" /> 
<Button Height="121" HorizontalAlignment="Left" Margin="217,61,0,0" Name="but2" VerticalAlignment="Top" Width="200" Background="#FF7D0000" BorderBrush="Black" Foreground="Red" BorderThickness="0" Padding="0" Style="{StaticResource ButtonStyle1}" /> 

解決方案

我改成了反映 「ContentContainer」,這是對象的名稱我想在我的風格內進行修改。

<VisualState x:Name="Disabled"> 
    <Storyboard> 
     <DoubleAnimation Duration="0" Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Opacity" To="1"/> 
    </Storyboard> 
</VisualState> 

回答

1

如果只是上面的代碼應該工作正常,但如果你設置任何的按鍵被禁止(IsEnabled = false),它會失敗,因爲在你的風格的禁用狀態,故事板正在尋找命名爲DisabledVisualElement動畫元素,

<VisualState x:Name="Disabled"> 
    <Storyboard> 
    <DoubleAnimation Duration="0" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity" To="1"/> 
    </Storyboard> 
</VisualState> 

然而,存在範圍不存在這樣的元素。

刪除此故事板或添加具有正確名稱的元素將解決您的問題。

+0

感謝您的提示。我認爲DisabledVisualElement是某種「常量」......事實並非如此。我修改它以反映 TekiusFanatikus 2012-04-14 13:55:11

+0

很高興幫助! – 2012-04-14 14:32:58