如何从类中更改WPF中的标签文本 [英] how to change text of labels in WPF from a class

查看:108
本文介绍了如何从类中更改WPF中的标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我对WPF来说是全新的。我试图从网站中提取一些东西。

i创建了一个表单Web浏览器控件,因为我不知道如何使用WPF web-browser.OK现在我已准备好所有前端。

我创建了一个不同的CLASS,并从那里完成所有操作

我正在创建动态网络浏览器,我能够导航和所有。但我想转移详细信息,如我目前正在工作的URL链接,我正在搜索的产品或fount回到WPF窗口标签控件

有没有这样的方法来做到这一点。

我用谷歌搜索它们并告诉许多事情实现接口这种方法和所有我从来不理解它。可以有人给出一些例子。

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.

推荐答案

好的,假设您创建了一个名为UrlManagerViewModel的类,其中包含您要显示的文本。你想让WPF通过数据绑定到这个类和字段来完成繁重的工作。好的,你是怎么做到的?



嗯,你需要知道的第一件事是WPF绑定使用一个名为的接口INotifyPropertyChanged 标识哪些项目已更改。它通过识别它绑定的项是否属于这种类型来实现,如果是,则接口包含一个名为 PropertyChanged 的特殊事件,用于标识属性何时更改。所以,我们在这里有第一部分 - 我们的数据绑定类必须实现 INotifyPropertyChanged 并且必须提高 PropertyChanged 基础属性更改时的事件。好吧,让我们一起来吧:
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; }
    set
    {
      if (url != value)
      {
        url = value;
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
          handler(this, new PropertyChangedEventArgs("Url");
        }
      }
    }
  }
}

所以,在Url更改时,在我们的基础数据类中,引发属性更改事件,使用属性的名称告诉WPF要查找哪个。好的,这似乎很容易,但肯定不是全部。我们如何实际的XAML视图知道这个类的位置?标签如何知道要连接的内容?



XAML难题的第一部分在于我们的观点需要通过一个名为 DataContext 的东西来连接这个类。有很多方法可以实现这一点,但最简单的方法是通过后面的代码。所有你需要的有创造要绑定到的类的实例,并在调用 InitializeComponent 之后,将其与 DataContext 关联。因此,在XAML的代码隐藏文件中,执行此操作(在此示例中,您已创建了一个名为UrlManagerView的用户控件):

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()
  {
    InitializeComponent();
    vm = new UrlManagerViewModel();
    this.Datacontext = vm;
  } 
}

好的,这是将两个类连接在一起,但是XAML如何知道要查看哪个属性?答案是,它使用了一种名为 MarkupExtension 的东西 - 别担心,你不需要知道这个阶段的其中一个是什么 - 具体来说,它使用Binding扩展。这是XAML编译器知道需要以特殊方式处理的一段文本。每当您看到包含 ={...}的XAML时,编译器就会知道它将使用 MarkupExtension 。在这种情况下,我们将使用 Binding 一个,所以我们假设你有一个 TextBlock 您希望将Url与之关联 - 在这种情况下,XAML如下所示:

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}" />

就是这样 - 我们将Url数据绑定到 TextBlock 。现在,每当基础Url发生更改时,WPF都会捕获更改通知,并且视图中的文本也会更改。



我刚才谈到的是某些内容的一部分叫做MVVM。如果你进入WPF,那么值得了解它是如何工作的。

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.


这篇关于如何从类中更改WPF中的标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆