2014-01-31 147 views
0

我已經按鈕創建編程:禁用默認按鈕點擊效果

Button button = new Button(); 
ImageBrush background = new ImageBrush(); 
background.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri(@"Assets/Image1.png", UriKind.Relative)); 
button.Background = background; 
button.Click += button_Click; 

現在我當我點擊該按鈕,我需要刪除默認的點擊效果和添加自己的形象,所以,當我在點擊按鈕應該顯示我添加的圖像。以上這

編輯

的有2級upvotes的作品,但它的更換背景圖片,但我想顯示兩個圖像,唯一的onclick應該前景圖像添加到我的按鈕。

我該如何實現這個目標?

回答

3

您需要一個帶按鈕的可視狀態玩,你可以用風格

只要看看下面的樣式

<Style x:Key="style_ColorButton" 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"> 
               <DiscreteObjectKeyFrame.Value> 
                <ImageBrush Stretch="Fill" ImageSource="/Images/ColorClicked.png"/> 
               </DiscreteObjectKeyFrame.Value> 
              </DiscreteObjectKeyFrame> 
             </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"> 
          <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風格的追問幫助」自己「處理您的需求,我已經在那裏提供一個圖像源。

使用此樣式的按鈕。

<Button Height="40" Width="40" BorderThickness="0" Name="btnAcceptCrop" Click="btnAcceptCrop_Click" Style="{StaticResource style_ColorButton}" Background="Black" ForeGround="White"/> 

設置樣式代碼

btnAcceptCrop.Style = (Style)App.Current.Resources["style_ColorButton"]; 

在你的頁面在頁面

聲明一個風格下的所有空間聲明

只是做一個標記

<phone:PhoneApplicationPage.Resources> 
</phone:PhoneApplicationPage.Resources> 

並在裏面聲明你的風格。

希望這會有所幫助。

+0

我怎樣才能以編程方式做到這一點,你能幫我嗎? – user2056563

+0

風格自動更改按鈕按動作時的圖像,您想要在代碼中寫什麼?改變風格?或製作一個?請添加更多代碼或快照。 –

+0

由於我有編程創建按鈕,當我點擊,我需要指示用戶,他點擊了那個按鈕,爲此我不想默認顏色,我想要一個圖像顯示在上面,我的按鈕有已經是一個背景圖片 – user2056563