Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
Check this picture of scenario 1:
http://www.fileden.com/files/2007/2/5/737151/Sc1.png[^]
 
So here the two buttons and the Click method that is being used in the example in Scenario 1.
<Button Tag="Purple5" Click="Button_Click_1"/>
<Button Name="P"/>
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;
                }              
            }
        }
 
Let's check Scenario 2:
http://www.fileden.com/files/2007/2/5/737151/Sc2.png[^]
 
Here, I create a button in the Code-behind file which looks like this.
public void GetGameButtons()
        {
                Button btn = new Button();
                btn.Name = "P";
 
                Grid.Children.Add(btn);    
        }
 
As far as I can see, I've created an identical button with a Name="P" like before but this time not in XAML. But the button with the Tag and the Click method won't work like before either, where is the problem? The button created in Code-behind won't be removed.
Posted 25-Feb-13 8:25am
Edited 25-Feb-13 9:47am
v3
Comments
Sergey Alexandrovich Kryukov at 25-Feb-13 14:31pm
   
What do you mean? There is no such think as "XAML but not C#". If your XAML creates some button, it is ultimately created in C# code. If you don't know where, it does not mean that this code does not exist. You just have no understanding of how it all works.
 
I just think that your approach with adding button is two ways and removing it is a kind of abuse. Better ask a question explaining your ultimate goals, what you effectively want to achieve. Without referring to your understanding of how it should be done...
 
—SA
Member 9808042 at 25-Feb-13 14:39pm
   
That's why I asked a question, I don't understand. Yeah, appearently there's something behind that I don't know, cause what I wrote works for buttons created in XAML bot not between them (XAML and C#). Thanks for you reply anyway...
Sergey Alexandrovich Kryukov at 25-Feb-13 14:44pm
   
Understood. I basically explained some background. XAML is merely used to auto-generate C# code and compiler it in the usual way. You can find that code in intermediate file. A good exercise anyway, take a look.
 
If you explain your ultimate goal, you can get a good chance for useful practical advice. Your problem does not look different.
 
—SA
Member 9808042 at 25-Feb-13 14:58pm
   
My question was not clear at all, my apology. Here's a picture that maybe can clear it up!
 
http://www.fileden.com/files/2007/2/5/737151/ex.png
Sergey Alexandrovich Kryukov at 25-Feb-13 15:04pm
   
Not really, but no need to apologize either. It's just fine. My advice will still be the same. You contaminate your question with your preexisting ideas you are preoccupied with. Try to formulate a clear problem, from the functionality perspective only. Don't operate XAML, code-behind at all. Describe the goals and behavior...
—SA
Member 9808042 at 25-Feb-13 15:49pm
   
I've tried to change the question, please take a look =)
ali rad at 25-Feb-13 14:38pm
   
your question isn't obvious.what do you want to do exactly?
Member 9808042 at 25-Feb-13 14:48pm
   
http://www.fileden.com/files/2007/2/5/737151/ex.png
 
When I click in the purple button (with the Tag) then the Button with the same name as the Tag will be removed. But the problem is that this doesn't work with the buttons I created in C# where I set the btn.Name = "Purple5" etc...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The button you create in code is not calling the Button_Click_1 because you are not hooking that event handler. Try adding
btn.Click += Button_Click_1();
to the block where you create the button.
 
WARNING! If you are going to remove a button, any button, you need to unhook the event handler. with the -= operator. Failure to do so will kill your performance. You have to be very careful that if you create or remove a button you also hook or unhook the event handler(s).
  Permalink  
Comments
Member 9808042 at 25-Feb-13 14:53pm
   
I'm sorry for not being clear in my question, the method is working as it should.
 
http://www.fileden.com/files/2007/2/5/737151/ex.png
 
Take a look at the picture, When clicking on the puple button, the grey button below in the column will be removed. But this does not work on the button above with the same name "Name=Purple". And I just don't get it...
Jason Gleim at 25-Feb-13 15:10pm
   
The picture isn't that useful as it is a diagram you have drawn rather than a screen-shot of the actual problem. Even referencing your diagram it still isn't really clear what exactly you are trying to do. You suggest the method is working but the body of your question leads us to believe it isn't. You, obviously, have a much better understanding of what you are trying to accomplish... you just haven't conveyed enough of that idea to us to understand where you are going.
 
To maybe address your question more directly, there is no difference between a button created in code and one create is xaml... they are both instances of the Button class. When a button is created in xaml, it is inserted into a specific spot in the visual tree based on how it is declared in the xaml. When you create a button in code it isn't present in the UI until you insert it into the visual tree as part of a parent control's children collection. You seem to be doing that although there are obviously big chunks of code missing from your post.
 
From the code you posted it also looks like you are sometimes assigning a value to the tag property and sometimes not. However, again, I can't really be sure if that is the case.
 
You might want to consider editing your question and maybe clarify a bit more for us. Everyone seems to be a bit confused by what you are describing and what the actual question/problem is. If you could better define that we will be able to more directly address the issue for you.
 
Thanks
Member 9808042 at 25-Feb-13 15:49pm
   
Thanks for your reply! Really appreciate it!
 
I've changed the question and put another pictures which I think will describe the problem better.
Jason Gleim at 25-Feb-13 16:04pm
   
The grid might be the issue... I've had problems with the rows in grid controls not being walked when searching for UI Elements. For some reasons the element finding methods don't like to walk down the control hierarchy of rows and cells. Probably because the cell content presenter has to be so generic... If I had to hazard a guess, that it where it might be.
 
It can be very tough to track this down if that is the case. You might have to go at it from a different angle. I would suggest you set a break on the if statement in the button handler and expand out the gr.children property in Quick Watch. See if you actually have the desired button in that collection. You might also unroll some of those statements (like add a line Button target = (UIElement)this.FindName(s);) to see if you actually get an object back and to try to expand out some of those collections.
 
I also STRONGLY recommend XAMLSpy. It will allow you to walk the visual tree at runtime so you can see where these UI elements are living. I imagine it is a case of the button not going where you think it is or that FindName is not recursively walking the controls collection of the row/column. XAMLSpy will break all that out for you. There is a free trial so maybe it will be enough to get you past this.
 
Good luck!
Member 9808042 at 25-Feb-13 16:25pm
   
I guess I have to change direction and find other solutions. I really appreciate your time and your help! Thanks!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

public void GetGameButtons()
        {
                Button btn = new Button();
                btn.Name = "P";
                grid.RegisterName(btn.Name, btn);
 
                Grid.Children.Add(btn);
        }
 
The extra line added solved my problem......so simple, yet not =)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 454
1 Maciej Los 404
2 CPallini 185
3 Sergey Alexandrovich Kryukov 182
4 Richard MacCutchan 120
0 OriginalGriff 5,634
1 Sergey Alexandrovich Kryukov 4,763
2 Peter Leow 2,954
3 Maciej Los 2,614
4 DamithSL 2,465


Advertise | Privacy | Mobile
Web04 | 2.8.140718.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid