2011-12-22 87 views
2

我有一個按鈕,其背景由LinearGradientBrush指定。我需要禁用此按鈕。所以,我只是將其isEnabled屬性設置爲"false"。但是,這也刪除了背景,因爲我的按鈕沒有邊框,所剩下的只是按鈕上的文字。下面是我的按鈕的XAML代碼:爲什麼禁用按鈕會刪除其背景顏色?

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

而當我設isEnabled"true"再次,背景回來,一切都和好如初。

這是應該發生什麼?
如果我只是想在不改變外觀的情況下使按鈕無法使用一段時間,該怎麼辦?

+0

很好的想象它的殘疾,而不是點擊和思考它爲什麼這樣表現。 – V4Vendetta 2011-12-22 08:52:36

+0

我同意。但是,這裏發生的事情是,你有一個漂亮的粉紅色實心矩形,裏面寫着一些文字。當它被禁用時,粉紅色的矩形會完全消失,只是文本仍然讓你想知道,「按鈕去哪裏了?等等,那個文本曾經是按鈕!」。它沒有給出這個按鈕被禁用的想法。 – Divya 2011-12-22 08:55:46

+0

禁用按鈕的預定義外觀與它們相關聯。您需要覆蓋這些設置 – Sandy 2011-12-22 08:57:14

回答

3

您需要編輯相應的VisualState以更改禁用按鈕的外觀。最簡單的方法是使用Expression Blend。

隨着窗體打開,右鍵單擊按鈕並選擇編輯模板 - <編輯一個副本。這將在定義該控件的VisualStates的頁面資源部分放置一大塊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元素的名稱爲「Disabled」。已經定義了「透明」背景,所以您只需編輯它即可。

如果您正在手動執行此操作,則需要在按鈕上設置Style="{StaticResource ButtonStyle1}"以使其生效。

+0

謝謝,我只是試試這個。 @ZombieSheep – Divya 2011-12-22 09:08:24

+0

如果不清楚,