C# 如何用属性绑定textblock

C# 如何用属性绑定textblock,c#,wpf,xaml,C#,Wpf,Xaml,我在.xaml中创建了一个textblock,并在.cs文件中声明了一个名为WrodName的属性。如何将该属性与textblock绑定。我需要我们在标记的xaml代码中编写的代码,即DataContext代码。直到现在我才想到这个 <TextBlock Text="{Binding WordName}"/> 从xaml设置DataContext: <Window x:Class="ApplicationName" xmlns="http://schemas.

我在.xaml中创建了一个textblock,并在.cs文件中声明了一个名为WrodName的属性。如何将该属性与textblock绑定。我需要我们在标记的xaml代码中编写的代码,即DataContext代码。直到现在我才想到这个

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

从xaml设置DataContext:

<Window x:Class="ApplicationName"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
或者像这样的代码隐藏:

this.DataContext = this;

但无论如何,我建议您将MVVM体系结构与INOtifyPropertyChanged事件一起使用。在这种情况下,只要属性设置为新值,UI就会更新。

从xaml设置DataContext:

<Window x:Class="ApplicationName"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
或者像这样的代码隐藏:

this.DataContext = this;

但无论如何,我建议您将MVVM体系结构与INOtifyPropertyChanged事件一起使用。在这种情况下,只要属性设置为新值,UI就会更新。

创建一个ViewModel类

public class MyViewModel
{
  public String WordName { get; set; }
}
然后在视图的代码隐藏中设置itemssource

public class MyView
{
   this.DataContext = new MyViewModel();

}

在代码或xaml中设置datacontext有不同的方法。确实是一个首选项。

创建一个ViewModel类

public class MyViewModel
{
  public String WordName { get; set; }
}
然后在视图的代码隐藏中设置itemssource

public class MyView
{
   this.DataContext = new MyViewModel();

}

在代码或xaml中设置datacontext有不同的方法。这确实是一个首选项。

您需要设置DataContext。一种快速的方法是在构造函数中设置它
DataContext=this

此外,您还需要在调用
initializeComponent()
之前设置属性,或者最好执行INotifyPropertyChanged。否则,设置属性时,用户界面将不会更新

见-

一个简单的例子

class YourClass : INotifyPropertyChanged
{
        private String _wordName;
        public String WordName 
        {
            get { return _wordName; }
            set
            {
                if (_wordName != value)
                {
                     _wordName= value;
                     OnPropertyChanged("WordName");
                }

            }
        }

        /// <summary>
        /// Raises the PropertyChanged notification in a thread safe manner
        /// </summary>
        /// <param name="propertyName"></param>
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

}
class YourClass:INotifyPropertyChanged
{
私有字符串_wordName;
公共字符串字名
{
获取{return\u wordName;}
设置
{
if(_wordName!=值)
{
_wordName=值;
OnPropertyChanged(“WordName”);
}
}
}
/// 
///以线程安全的方式引发PropertyChanged通知
/// 
/// 
私有void OnPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
#区域INotifyProperty更改成员
公共事件属性更改事件处理程序属性更改;
#端区
}

您需要设置DataContext。一种快速的方法是在构造函数中设置它
DataContext=this

此外,您还需要在调用
initializeComponent()
之前设置属性,或者最好执行INotifyPropertyChanged。否则,设置属性时,用户界面将不会更新

见-

一个简单的例子

class YourClass : INotifyPropertyChanged
{
        private String _wordName;
        public String WordName 
        {
            get { return _wordName; }
            set
            {
                if (_wordName != value)
                {
                     _wordName= value;
                     OnPropertyChanged("WordName");
                }

            }
        }

        /// <summary>
        /// Raises the PropertyChanged notification in a thread safe manner
        /// </summary>
        /// <param name="propertyName"></param>
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

}
class YourClass:INotifyPropertyChanged
{
私有字符串_wordName;
公共字符串字名
{
获取{return\u wordName;}
设置
{
if(_wordName!=值)
{
_wordName=值;
OnPropertyChanged(“WordName”);
}
}
}
/// 
///以线程安全的方式引发PropertyChanged通知
/// 
/// 
私有void OnPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
#区域INotifyProperty更改成员
公共事件属性更改事件处理程序属性更改;
#端区
}

这将绑定到您的财产:

<TextBlock Text="{Binding WordName,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"/>

这将绑定到您的属性:

<TextBlock Text="{Binding WordName,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"/>

还有,按元素名。首先命名窗口:

<Window x:Class="ApplicationName" x:Name=AnyName ...

也可以按元素名称。首先命名窗口:

<Window x:Class="ApplicationName" x:Name=AnyName ...

让我试试这个,然后给你回复。同时,我也要感谢我没有做到这一点。我正在初始化构造器中wordname的值,所以它是否可以?您需要在初始化()或实现InotifyPropertyChangeDyeah之前分配该值。我刚刚再次检查了您的答案,了解初始化前设置属性的信息。请让我尝试一下,然后返回给您。同时,我也要感谢我没有做到这一点。我正在初始化构造器中wordname的值,这样可以吗?在初始化()或实现InotifyPropertyChangeDyeah之前,您需要分配该值。我刚刚再次检查了您的答案,以了解在初始化之前设置属性的信息()这段代码有很多。它帮助我理解了如何实现INotifyPropertyChangedYeah,如果没有它,即使绑定正确,UI也不会更新。如果您在
InitializeComponent()
之前设置属性,但之后它不会更新,则可能是INotifyPropertyChange有问题。这是我们在生产代码中使用的模式。我认为它工作得很好。对于这段代码,Thanx非常有用。它帮助我理解了如何实现INotifyPropertyChangedYeah,如果没有它,即使绑定正确,UI也不会更新。如果您在
InitializeComponent()
之前设置属性,但之后它不会更新,则可能是INotifyPropertyChange有问题。这是我们在生产代码中使用的模式。我认为它工作得很好。