2013-07-03 49 views
0

我正在使用下面的ControlTemplate爲我的應用程序中的圖像按鈕。設置圖像爲禁用按鈕在Windows Phone 8

控制模板App.xaml如下。

<!--Below is the style for all ImageButtons in this project --> 
    <Style x:Key="ButtonStyleIB" 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 x:Name="grid" Background="Transparent"> 
         <Grid.Projection> 
          <PlaneProjection/> 
         </Grid.Projection> 
         <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> 
             <DoubleAnimation Duration="0" To="-25" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="grid" /> 
             <!--d:IsOptimized="True"--> 
             <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.CenterOfRotationX)" Storyboard.TargetName="grid" /> 
             <!--d:IsOptimized="True"--> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

現在在我的頁面我創建按鈕如下。

<Button x:Name="saveButton" Style ="{StaticResource ButtonStyleIB}" VerticalAlignment="Center" Click="save_button_clicked"> 
      <Image Source="/STCDirectory;component/Images/save.png" Stretch="Fill" /> 
     </Button> 

高興這是好的。所以當我運行應用程序時,顯示圖像的按鈕。 它工作正常。

現在基於一些內部條件我想禁用圖像按鈕。 因此,爲了禁用我使用下面的代碼的按鈕。

saveButton.IsEnabled = false; 

因此,然後圖像按鈕不能點擊按照上述步驟。

但是我想在每次禁用按鈕時更改圖像。

我怎麼能做到這一點。

回答

0

不要給按鈕上的圖像直接使用 你可以用一個字符串屬性來綁定它,然後在按鈕上點擊你可以改變那個字符串到另一個uri然後它會改變按鈕的圖像到新圖像。

我希望這可以幫助...

+0

我是新手windows phone development,能否詳細解釋一下。 –

+0

你使用ViewModel? –

0

您可以使用綁定的ImageSource定義..first這樣您的按鈕做。

<Button Grid.Row="1" x:Name="saveButton" Style ="{StaticResource ButtonStyleIB}" VerticalAlignment="Center" Click="saveButton_Click_1" Width="300" Height="50"> 
     <Image Source="{Binding ImageSource}" Stretch="Fill" /> 
    </Button> 

並在你的page.xaml.cs中創建屬性ImageSource就像這樣。

public partial class MainPage : PhoneApplicationPage , INotifyPropertyChanged 
{ 
    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 
     ImageSource = "/Assets/1.jpg"; 
     this.DataContext = this; 
    } 
    private string _ImageSource; 
    public string ImageSource 
    { 
     get 
     { 
      return _ImageSource; 
     } 
     set 
     { 
      _ImageSource = value; 
      FirePropertyChanged("ImageSource"); 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected void FirePropertyChanged(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    private void saveButton_Click_1(object sender, RoutedEventArgs e) 
    { 
     ImageSource = "/Assets/2.jpg"; 
     saveButton.IsEnabled = false; 
    } 


} 

這裏1.jpg和2.jpg是我可以切換的圖像。