Click here to Skip to main content
13,451,731 members (47,253 online)
Rate this:
Please Sign up or sign in to vote.
See more:
hi all
i am totally new to WPF. I am trying to extract few thing from websites.
i have created a forms web-browser control as i was not sure how to work with WPF web-browser.OK now i have all the front end ready.
I have created a different CLASS and doing all the operations from there
I am creating dynamic web-browser and i am able to navigate and all. but i want to transfer the details like the URL link i am currently working, the product i am searching or fount back to the WPF window label control
is there any such way to do that.
I googled it and they told many thing implement that interface this method and all i never understood it. can some one give some example as of such thing.
Posted 7-Feb-13 22:57pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Okay, suppose you have created a class called UrlManagerViewModel that contains the text that you want to display. You want to let WPF do the heavy lifting by data binding to this class and field. Okay, how do you do this?

Well, the first thing you need to know is that WPF binding uses an interface called INotifyPropertyChanged to identify what individual items have changed. It does this by identifying if the item it is binding to is of this type, if it is, the interface contains a special event called PropertyChanged that identifies when a property changes. So, we have the first part of the puzzle here - our data bound class must implement INotifyPropertyChanged and it must raise the PropertyChanged event when the underlying property changes. Okay, let's pull that together:
public class UrlManagerViewModel : INotifyPropertyChanged
  private string url;
  public event PropertyChangedEventHandler PropertyChanged;
  public string Url
    get { return url; }
      if (url != value)
        url = value;
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
          handler(this, new PropertyChangedEventArgs("Url");
So, in our underlying data class whenever Url changes, the property changed event is raised, using the name of the property to tell WPF which one to look up. Okay, that seems easy enough, but surely that's not all. How does our actual XAML view know where this class is? How does the label know what to hook up to?

The first part of the XAML puzzle lies in the fact that our view needs to hook up to this class through something called a DataContext. There are many ways to achieve this, but the simplest way to do it is through the code behind. All you need to do there is create an instance of the class you want to bind to, and after the call to InitializeComponent, associate it with the DataContext. So, in the code behind file for your XAML, do this (in this example, you've created a user control called UrlManagerView):
public class UrlManagerView : UserControl
  private UrlManagerViewModel vm;
  public UserManagerView()
    vm = new UrlManagerViewModel();
    this.Datacontext = vm;
Okay, that's hooked the two classes together, but how does the XAML know which property to look at? The answer is that it uses something called a MarkupExtension - don't worry, you don't need to know what one of these is at this stage - specifically, it uses the Binding extension. This is a piece of text that the XAML compiler knows needs to be treated in a special way. Whenever you see XAML that contains ="{...}", the compiler knows that it will use a MarkupExtension. In this case, we are going to use the Binding one, so let's assume that you have a TextBlock that you want to associate the Url with - the XAML in this case looks like this:
<TextBlock Text="{Binding Url}" />
And that's it - we have databound the Url to the TextBlock. Now, whenever the underlying Url changes, the change notification is caught by WPF and the text is changed in the view.

What I have just talked through is part of something called MVVM. If you're getting into WPF, it's worth learning how this works.
Manu_vish 8-Feb-13 5:46am
Thank you very much, this was really very good explanation. I will try this and let you know how it came out. if there are any links which explains the WPF controls please let me know. i know the overview. but details like these if there are any
Manu_vish 8-Feb-13 5:58am
one more thing as i am using more than one label and updating information like URL,Product number,Manufacture so i must create 3 string and link them to the 3 labels rite.
Pete O'Hanlon 8-Feb-13 5:59am
That's correct.
Manu_vish 8-Feb-13 6:05am
can you please give me some links or suggest books about WPF. i want to start building in that rather than forms
Pete O'Hanlon 8-Feb-13 6:15am
Adam Nathan's WPF Unleashed is a great book on WPF. Check out articles by Sacha Barber and Josh Smith here on Code Project.
Manu_vish 8-Feb-13 6:31am
can you please write the code in, i tried to convert and write but unable to write the handler part getting confused. thanks once again. and if this is not inappropriate or not following rules. if i could have your public ID where i could clear or get in touch for any small doubts would be really helpful
Pete O'Hanlon 8-Feb-13 6:33am
No I can't, and I don't give out my details. Try running the code through a C# to VB.NET converter like the one on Telerik.
Manu_vish 11-Feb-13 6:40am
i have a doubt you have to create a new class for the control? i am not clear of this part can you please tell a little more
" in the code behind file for your XAML, do this (in this example, you've created a user control called UrlManagerView):"
That url manager view is window class??
Pete O'Hanlon 11-Feb-13 7:05am
No - the UrlManagerView is a UserControl - that's why I said user control.
Manu_vish 25-Feb-13 1:38am
i am extremely sorry. but i am kind of struck in this question.
if you could help me a little

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03-2016 | 2.8.180318.3 | Last Updated 8 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100