Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to compare buttons created in XAML and those in C#?

$
0
0

I got this grid where I put a bunch of buttons in. In one (empty) "special column", I create buttons in C# with following method:

public void GetGameButtons()
        {
            for (int i = 1; i < 2; i++)
            {
                Button btn = new Button();
                btn.Name = "Purple5";
                btn.Content = "Purple5";
                Grid.SetRow(btn, i);
                Grid.SetColumn(btn, 7);
                gr.Children.Add(btn);
            }    
        }

Right now, I only create 1 button in that column. However, what I want to achieve is when I click on another button on the grid with the corresponding Tag, in this case Tag="Purple", if the Tag and Name is the same, then the button I created in C# should be removed like this:

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (sender != null)
            {
                string s = btn.Tag.ToString();
                if (gr.Children.Contains((UIElement)this.FindName(s)))
                {
                    gr.Children.Remove((UIElement)this.FindName(s));
                    btn.IsEnabled = false;
                }              
            }
        }

Somehow, my Button_Click_1 won't work with buttons created dynamically but will work with buttons created in XAML.

<Button Name="Purple5" Content="5" Grid.Column="7" Grid.Row="1"/>

For me, they are identical...but why doesn't it work with the buttons created in C#?
 
This is the button that is "clickable"

<Button Tag="Purple5" Background="Purple" Content="5" Grid.Column="5" Grid.Row="5" Click="Button_Click_1"/>

Could I do it in another way? Compare with content or something? Help!

Here's a picture : http://www.fileden.com/files/2007/2/5/737151/ex.png

Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>