如何将TextBlock设置为属性值?

时间:2022-01-04 15:49:41

I used this tutorial to build a custom control. Now, I'd like to add a simple message (a textblock) to the user control to give the user some guidance. I think I can add a public property, like FileName in the tutorial, but how do I wire up the textblock's Text property to the property in the code behind? And then make sure the textblock message updates if the property changes.

我使用本教程构建自定义控件。现在,我想向用户控件添加一条简单的消息(文本块),以便为用户提供一些指导。我想我可以在教程中添加一个公共属性,比如FileName,但是如何将textblock的Text属性连接到后面代码中的属性?然后确保在属性更改时更新textblock消息。

I like the idea of being able to set the message in code, via a property, because I will likely have multiple controls of this custom control type on a page. I'm just a bit stumped on wiring it up.

我喜欢能够通过属性在代码中设置消息的想法,因为我可能在页面上有多个这种自定义控件类型的控件。我只是有点难以接线。

Thanks!

1 个解决方案

#1


1  

This would be your code behind, which implements INotifyPropertyChanged:

这将是您的代码,它实现了INotifyPropertyChanged:

/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window, INotifyPropertyChanged{    public event PropertyChangedEventHandler PropertyChanged;    private string _fileName;    /// <summary>    /// Get/Set the FileName property. Raises property changed event.    /// </summary>    public string FileName    {        get { return _fileName; }        set        {            if (_fileName != value)            {                _fileName = value;                RaisePropertyChanged("FileName");            }        }    }    public MainWindow()    {        DataContext = this;        FileName = "Testing.txt";    }    private void RaisePropertyChanged(string propertyName)    {        if (PropertyChanged != null)        {            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));        }    }         }

This would be your XAML that binds to the property:

这将是绑定到属性的XAML:

<TextBlock Text="{Binding FileName}" />

EDIT:

Added DataContext = this; i don't normally bind to the code behind (I use MVVM).

添加了DataContext = this;我通常不会绑定到后面的代码(我使用MVVM)。

#1


1  

This would be your code behind, which implements INotifyPropertyChanged:

这将是您的代码,它实现了INotifyPropertyChanged:

/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window, INotifyPropertyChanged{    public event PropertyChangedEventHandler PropertyChanged;    private string _fileName;    /// <summary>    /// Get/Set the FileName property. Raises property changed event.    /// </summary>    public string FileName    {        get { return _fileName; }        set        {            if (_fileName != value)            {                _fileName = value;                RaisePropertyChanged("FileName");            }        }    }    public MainWindow()    {        DataContext = this;        FileName = "Testing.txt";    }    private void RaisePropertyChanged(string propertyName)    {        if (PropertyChanged != null)        {            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));        }    }         }

This would be your XAML that binds to the property:

这将是绑定到属性的XAML:

<TextBlock Text="{Binding FileName}" />

EDIT:

Added DataContext = this; i don't normally bind to the code behind (I use MVVM).

添加了DataContext = this;我通常不会绑定到后面的代码(我使用MVVM)。