2012-04-14 9 views
1

私のすべてのボタンにスタイルを適用しようとしています。すぐに私は2番目のボタンに自分のスタイルを割り当て、私のアプリを実行しようとして、私はエラーを取得する:単一のボタンに自分のスタイルを適用する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

+0

喜んで助けました! –

関連する問題