2011-12-22 7 views
2

背景がLinearGradientBrushで指定されたボタンがあります。このボタンを無効にする必要があります。だから、isEnabledプロパティを"false"に設定するだけです。しかし、それも背景を削除し、私のボタンは境界線を持たないので、私が残したのはボタンのテキストだけです。ボタンを無効にすると背景色が削除されるのはなぜですか?

<Button x:Name="create" Content="Create a new one?" Margin="12, 0, 12, 0" ClickMode="Release" Click="create_click" MinWidth="330" MinHeight="50" BorderThickness="0" Height="110" Foreground="Black"> 
    <Button.Background> 
     <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
      <GradientStop Color="#FFFA2FC5" Offset="0.279" /> 
      <GradientStop Color="#FF5904A0" Offset="1" /> 
     </LinearGradientBrush> 
    </Button.Background> 
</Button> 

そして私は再び"true"isEnabledを設定すると、背景が戻ってくると、すべてが再び良いです:ここに私のボタンのXAMLコードです。

これは何が起こるはずですか?
ボタンの外観を変更せずにしばらくボタンを非機能にしたい場合はどうすればよいですか?

+0

このように動作している理由をクリックして熟考するのではなく、無効にしていることを視覚化するのがよいでしょう。 – V4Vendetta

+0

私は同意します。しかし、ここで起こっているのは、内部にテキストが書かれたすてきなピンクの矩形があることです。それが無効になると、ピンク色の矩形が完全に消えて、テキストだけが残っています。「そのボタンはどこに行きましたか?ああ、そのテキストはボタンでした!ボタンが無効になっているという考えはありません。 – Divya

+0

無効ボタンには、あらかじめ定義された外観が関連付けられています。あなたはそれらの設定を無効にする必要があります – Sandy

答えて

3

無効なボタンの外観を変更するには、適切なVisualStateを編集する必要があります。これを行う最も簡単な方法は、Expression Blendを使用することです。

フォームを開いた状態で、ボタンを右クリックし、[テンプレートの編集]を選択します。<コピーを編集します。これにより、コントロールのVisualStateを定義するページリソースセクションにXAMLのチャンクが配置され、インスタンスのスタイルが新しく定義されたスタイルにバインドされます。 Xamlは次のようになります...

<Style x:Key="ButtonStyle1" TargetType="Button"> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/> 
    <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> 
    <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/> 
    <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/> 
    <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/> 
    <Setter Property="Padding" Value="10,3,10,5"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Grid Background="Transparent"> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <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="Background" Storyboard.TargetName="ButtonBackground"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}"> 
         <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
        </Border> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

あなたが関係するビットは、名前が「無効」のVisualState要素です。すでに透明な背景が定義されているので、編集するだけで済みます。

これを手動で行う場合は、この設定を有効にするには、ボタンにStyle="{StaticResource ButtonStyle1}"を設定する必要があります。

+0

ありがとう、私はこれを試してみましょう。 @ ZombieSheep – Divya

+0

明確でない場合は、