2

I am creating a tic tac toe game for windows 8.1 store using xaml and c# and was trying to change the X and O button on click. Initially, I have a blank image set in the xaml for the button and in the button_click event, I am trying to change the image source. Here is a snippet of my code:

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;
    }

I have read a few articles online and some of them suggested that I set the build action of my image to Resource, however I do not have that as an option. I have PRIResource and Embedded Resource as an option. I have very limited knowledge of mvvm and triggers and hence would like to solve the problem using wpf itself and not in xaml.

I am using VS Studio Professional 2013

5
  • Why not use a ToggleButton with X & O for the IsChecked true/false states? Seems like extra work this way. Commented Apr 23, 2014 at 2:35
  • if i use toggle button how can i determine which image to display as once a player can select X and another turn the player can select O. I think I would have to bind the data in this case right? Also initially i would need the button to display a blank image right hence that makes it 3 states. blank , X and O Commented Apr 23, 2014 at 3:00
  • Well the 3 state thing shouldn't pose much challenge. Then you could just throw their state down the tree based on which player is up. Commented Apr 23, 2014 at 3:02
  • so this would require me to bind the data or use triggers? I am not really sure how to go about it . I would set isThreeState property to True and how would I set the value of each state in the xaml or .cs file ? Commented Apr 23, 2014 at 3:12
  • Ya you could bind the IsChecked state twoway and base the players click event on whether its player1 or player2 will put it in IsChecked=True or False or x:Null, but I may be over complicating it also lol, would have to stew on this one for a minute. Commented Apr 23, 2014 at 3:31

2 Answers 2

3

Background is not displayed when the button is disabled. You should rather have an Image as the button content:

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;
}

I would also strongly suggest you learn and start using XAML and binding instead of avoiding it. You're actually "fighting" against WPF the way you are using it and making it much harder for yourself. You might find it superior to Windows Forms if you use it as intended.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. This works perfectly. Yes I shall start learning binding and XAML more
1

I have done something very similar in WPF, not tic tac toe, but having multiple data grids, each with a different background occupying the exact same position, and enabling/disabling then with a button. I'd write some code down, but I don't know wpf off the top of my head like I do winforms, and they are very different (Main difference is wpf is crap in comparison).

But something like:

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

    }
    button1.Visibility = hidden;
}

Then just put the images inside the grids, or labels, or w/e

2 Comments

Yes thank you for your suggestion. I was doing the same when I realized I will have to make many grids or buttons in my case and set their visibility to collapsed or visible. Seems like a lot of xaml code and hence thought of changing the image in the .cs file itself
I see. WPF is complicated, in winforms it'd be as easy as going: button1.Image = Namespace.Properties.Resources.imageName;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.