I've been trying to understand how to use INotifyPropertyChanged interface inside a class that i'm using but i have a problem understanding the link between the event raised inside the class e the INotifyPropertyChanged that, from what i understood, is sent to the UI...
So i have a class with a property that can have two values (two string: "EXP" and "INS")...
Depending on the value of this property I need to use a different list and different constants inside the logic of the class, but since the information about that property will be helpful also in the UI i thought that would have been a good idea to sue the INotifyPropertyChanged inside the class....
Just i don't get if I need to create an event for what i have to use inside the class AND INotifyPC for the UI, or if it'ìs possible to use simply INotifyPC to achieve both things....
if the class has the structure suggesting by MSDN
public event PropertyChangedEventHandler PropertyChanged;
public string myProperty { get { return name; } set { name = value; OnPropertyChanged(String); } }
Where exactly i should subscribe this event in order to modify other property inside my class (like which list i'm using, which constants, etc...)
Thanks a lot, I.
VulpesPosted May 3, 2011, 4:47 AM
IvanPosted May 7, 2011, 9:40 AM
Mahesh ChandPosted May 7, 2011, 9:08 AM
VulpesPosted May 7, 2011, 8:30 AM
First, add another TextBox, textBox2, to the project.
Now change the code behind I posted earlier, as follows:
public string MyString { get; set; }
Finally, change the XAML for textBox2 as follows (positional properties will of course be different):
When you build and run, the string "Vulpes" should now appear in textBox2.
IvanPosted May 7, 2011, 6:49 AM
Just last question then i promise i'll stop :)..
Is there a way to do this way back?... I mean i have to get a handle to an object instantiated by the WPF layer in the code behind...
Can i get a handle in the WPF to an object already instantiated in the code behind?....
thanks...
VulpesPosted May 3, 2011, 9:32 AM
IvanPosted May 3, 2011, 9:13 AM
then so if i understood well, the XAML has a binding to the class, not to a specific object... is there aa way to bind a control of the XAML to a specific object i created on the code?... is the famous "Separation of Concerns" connected to this structure?....
IvanPosted May 3, 2011, 4:12 AM
thanks... anyway i still don't understand what you tried to explained me.
I thought that having the class was enough for the main window to instantiate an object,so why the main window should search the same object as a resource of somthing i declared in the xaml?..
I know i can't express my self good enough.
But all the examples i saw on the web they lack this part... there are a lot of examples about class Person with name, etc...
so i can boud the property name to a textbox and the textbox will change the content based on the name of the object person that i created in the main window...
Are you saying that also in this case i have to put
Person p = new Person()
and then after the inizialization:
p = (Person)this.FindResource....
like this?.. for every object that i use in the main window and is binded to some control?
VulpesPosted May 2, 2011, 6:25 PM
The problem is that you need to get a reference to the encoder object in the code behind and the only way to do that is to use FindResource() because the code behind knows nothing about that object.
IvanPosted May 2, 2011, 6:10 PM
so should be smt like
VulpesPosted May 2, 2011, 6:04 PM
and you're then binding the textbox to the ValueAng property of this instance.
You therefore need to call the Start() method of this instance from the code behind rather than create a new one which will be unbound.
IvanPosted May 2, 2011, 5:58 PM
but my object is encoder.cs, and in windows.xaml.cs (that is the main right?)
i have encoder en = new encoder()...
so i basically created my object in code like i used to be with windows form application....
where's exactly the difference?....
thanks again.. I
VulpesPosted May 2, 2011, 5:52 PM
You can, of course, create objects in the usual way from the code behind itself.
WPF is complicated - an order of magnitude more so than Windows Forms - but that's the price you pay for the additional power it gives you.
IvanPosted May 2, 2011, 5:45 PM
to be honest that never crossed my mind... (by the way, is there any reason why it shoudl behave like that?... If i create any object on the main window i have to get the reference form the Static resource?... I'm not very good but doens't it sound complex??... )..
I'll try tomorrow morning as soon as i have a working visual studio under my hands (i just discovered that Vista Home and VS2010 don't like each other... )
thanks again, tomorrow i wil let you know...
I.
VulpesPosted May 2, 2011, 5:38 PM
IvanPosted May 2, 2011, 4:02 PM
That encoder it's just a little example i made to make myself things clearer, but i have to face a bigger problem where i need the databinding use...
VulpesPosted May 2, 2011, 3:58 PM
IvanPosted May 2, 2011, 3:37 PM
encoder en = new encoder();
and under the click event button
en.Start();
did i miss something?
VulpesPosted May 2, 2011, 2:44 PM
IvanPosted May 2, 2011, 12:10 PM
thank you for the fast reply... I think i get it (I'm not sure anyway)...
but really i wanted to ask smt more specific, (of course i can't find the link in your answer becasue i'm new in all of this)...
what if i have a Property inside the class, and i need that when it changes the event can both make smt happens inside the class (like another pèroperty changes) and change some visual controls?...
I will try to post an example so maybe i can be clearer,....
I have this encoder class that uses a timer and simulates an angle encoder... so when it arrives to 360° it should both writes on the textbox smt and changes some other property (in this example i just put to zero the value again...)
Is is the right way to use the InotifyPropertyChanged and above all... Is it the right way of binding on the UI cause on the textBox i can't see anything....
here's the code...
Thanks, I.
---------
the xaml is
and the class is
VulpesPosted Apr 29, 2011, 12:04 PM