2013-07-03 48 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是我可以切换的图像。