2014-04-23 40 views
2

我正在为使用xaml和c#的windows 8.1商店创建一个tic tac脚趾游戏,并试图在点击时更改X和O按钮。最初,我在xaml中为按钮设置了一个空白图像,在button_click事件中,我正在尝试更改图像源。这里是我的代码片段:在wpf c中单击更改按钮图像#

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     Button btn = sender as Button; 
     if (turn == 1) 
     { 
      BitmapImage bmp = new BitmapImage(); 
      Uri u = new Uri("ms-appx:/Images/O_logo.png", UriKind.RelativeOrAbsolute); 
      bmp.UriSource = u; 
      ImageBrush i = new ImageBrush(); 
      i.ImageSource = bmp; 
      btn.Background = i; 
     } 
     else 
     { 
      BitmapImage bmp = new BitmapImage(); 
      Uri u = new Uri("ms-appx:/Images/X_logo.png", UriKind.RelativeOrAbsolute); 
      bmp.UriSource = u; 
      ImageBrush i = new ImageBrush(); 
      i.ImageSource = bmp; 
      btn.Background = i; 

     } 
     btn.IsEnabled = false; 
     //win(btn.Content.ToString()); 
     turn += 1; 
     if (turn > 2) 
      turn = 1; 
    } 

我在网上读了几篇文章,其中一些建议,我把我的图像资源的累积作用,但是我没有那么作为一个选项。我有PRIResource和Embedded Resource作为选项。我对mvvm和触发器的知识非常有限,因此希望使用wpf本身而不是xaml来解决问题。

我使用VS Studio专业2013

+0

为什么不使用带有X&O的'ToggleButton'作为'IsChecked'真/假状态?看起来像这样的额外工作。 –

+0

如果我使用切换按钮我怎样才能确定显示哪个图像,因为一旦玩家可以选择X而另一个玩家可以选择O.我想我必须在这种情况下绑定数据吧?另外最初我需要按钮来显示一个空白的图像,因此使它成为3个状态。空白,X和O – justin3250

+0

那么[3状态](http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.isthreestate(v = vs.110).aspx)事情不应该构成太大的挑战。然后,您可以根据哪个玩家注册,将他们的状态放在树上。 –

回答

3

Background当按钮被禁用时不显示。你还是有一个Image作为按钮的内容:

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    Button btn = sender as Button; 
    if (turn == 1) 
    { 
     BitmapImage bmp = new BitmapImage(); 
     Uri u = new Uri("ms-appx:/Images/O_logo.png", UriKind.RelativeOrAbsolute); 
     bmp.UriSource = u; 
     // NOTE: change starts here 
     Image i = new Image(); 
     i.Source = bmp; 
     btn.Content = i; 
    } 
    else 
    { 
     BitmapImage bmp = new BitmapImage(); 
     Uri u = new Uri("ms-appx:/Images/X_logo.png", UriKind.RelativeOrAbsolute); 
     bmp.UriSource = u; 
     // NOTE: change starts here 
     Image i = new Image(); 
     i.Source = bmp; 
     btn.Content = i; 

    } 
    btn.IsEnabled = false; 
    //win(btn.Content.ToString()); 
    turn += 1; 
    if (turn > 2) 
     turn = 1; 
} 

我也强烈建议你学习并开始使用XAML,而是结合避免它。你实际上是在与你使用WPF的方式“战斗”,并且让自己变得更加困难。如果按照预期使用它,您可能会发现它优于Windows窗体。

+0

非常感谢。这工作完美。是的,我将开始更多地学习绑定和XAML – justin3250

1

我已经做了在WPF非常类似,不井字,但具有多个数据网格的东西,每一个不同的背景占据完全相同的位置,并启用/禁用然后用一个按钮。我会写一些代码,但我不知道wpf像我做winform一样,而且他们非常不同(主要区别在于wpf是比较糟糕的)。

但这样的:

button1_click(object sender, EventArgs e) 
{ 
    if (turn == 1) 
    { 
     gridX.Visibility = visible; 
    } 
    else 
    { 
     gridO.Visibility = visible; 

    } 
    button1.Visibility = hidden; 
} 

然后只是把图像的网格,或标签内,或W/E

+0

是的,谢谢你的建议。当我意识到必须在我的案例中制作很多网格或按钮并将其可见性设置为折叠或可见时,我也这样做了。看起来像很多xaml代码,因此想到改变.cs文件本身的图像 – justin3250

+0

我明白了。 WPF是复杂的,在winforms中它会像去一样简单:'button1.Image = Namespace.Properties.Resources.imageName;' – Shadow