2015-08-20 50 views
1

我嘗試點擊相同的按鈕時,按鈕的兩幅圖像之間進行切換時 我的XAML代碼是上的按鈕改變圖像點擊XAML

<Button x:Name="BidderOne" 
      Click="BidderOne_Click" Height="5" 
      Grid.Row="2" 
      BorderBrush="#FFE7E3E3" 
      HorizontalAlignment="Center" 
      VerticalAlignment="Center" 
      Margin="350,0,0,280" 
      > 
     <Button.Template> 
      <ControlTemplate> 
       <Image x:Name="BidderOneImage" 
         Source="/Assets/Star.png" 

         Width="50"/> 
      </ControlTemplate> 
     </Button.Template> 
    </Button> 

林不知道,在我的課寫什麼,但我發現,沿着這

private void BidderOne_Click(object sender, RoutedEventArgs e) 
    { 
     var selectedBidder = sender as Button; 
     // Button btn = new Button(); 
     // btn = BidderOne; 
     ControlTemplate dt = BidderOne.Template; 
     Image btnImage = (Image)dt.TargetType = ; 
     var img = (Image)(selectedBidder.ContentTemplateRoot as StackPanel).Children[0]; 
     var uri = new Uri("/Assetes/ClubsLogo.png", UriKind.Relative); 
    // var sri = Windows.UI.Xaml.Application.("Assetes/ClubsLogo.png", UriKind.Relative); 
     imgOn.UriSource=uri; 
     img.Source = imgOn; 
    } 

回答

0

不建議在運行時更改ControlTemplateControl。對於這些情況,您應該創建一個自定義控件。

我已經寫了一個簡單的ImageButton,這樣你就可以得到創建你的問題所需的東西的想法。

下面是代碼:

public sealed class ImageButton : Button 
{ 
    private Image _image; 

    public ImageButton() 
    { 
     this.DefaultStyleKey = typeof (Button); 

     _image = new Image(); 
    } 

    protected override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 

     this.Content = _image; 
     OnImage1SourceChanged(null, null); 
    } 

    public ImageSource Image1Source 
    { 
     get { return (ImageSource)GetValue(Image1SourceProperty); } 
     set { SetValue(Image1SourceProperty, value); } 
    } 

    public static readonly DependencyProperty Image1SourceProperty = 
     DependencyProperty.Register("Image1Source", 
      typeof (ImageSource), 
      typeof (ImageButton), 
      new PropertyMetadata(null, OnImage1SourceChangedCallback)); 

    private static void OnImage1SourceChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var imageButton = d as ImageButton; 
     if (imageButton == null) return; 
     imageButton.OnImage1SourceChanged((ImageSource)e.OldValue, (ImageSource)e.NewValue); 
    } 

    private void OnImage1SourceChanged(ImageSource oldValue, ImageSource newValue) 
    { 
     if (_image != null) 
     { 
      _image.Source = Image1Source; 
     } 
    } 

    public ImageSource Image2Source 
    { 
     get { return (ImageSource)GetValue(Image2SourceProperty); } 
     set { SetValue(Image2SourceProperty, value); } 
    } 

    public static readonly DependencyProperty Image2SourceProperty = 
     DependencyProperty.Register("Image2Source", 
      typeof (ImageSource), 
      typeof (ImageButton), 
      new PropertyMetadata(null)); 

    protected override void OnTapped(TappedRoutedEventArgs e) 
    { 
     base.OnTapped(e); 

     if (_image != null) 
     { 
      _image.Source = Image2Source; 
     } 
    } 
} 

並且你使用這樣的:

<controls:ImageButton Width="60" Height="60" Image1Source="/Assets/Images/Chat/ic_comment_favorite_yellow.png" Image2Source="/Assets/Images/Flags/afghanistan.png"/> 
0

線的東西試試這個

EDITED

private void BidderOne_Click(object sender, RoutedEventArgs e) { 
    var selectedBidder = sender as Button; 
    // WRONG -> Image image = selectedBidder.Template.FindName("BidderOneImage", selectedBidder) as Image; 
    Image image = VisualTreeHelper.GetChild(selectedBidder, 0) as Image; 
    image.Source = new BitmapImage(new Uri("NEW IMAGE URI")); 
} 
+0

即時得到一個語法錯誤。 「ControlTemplate不包含FindName的定義,也沒有擴展方法 – user2800591

+0

Ups ...我沒有看到windows-phone-8.1標記,很抱歉... – Nacho

+0

有沒有辦法解決它? – user2800591