简体   繁体   English

如何在 wpf 中为 label 设置绑定

[英]how to set binding for the label in wpf

I need to bind a property to a label.我需要将属性绑定到 label。 i have written the following code: xaml for the label is我编写了以下代码: label 的 xaml 是

<Label Canvas.Left="807.3" Canvas.Top="148.9" Height="33.567" x:Name="label2"
       Width="98" FontFamily="Tw Cen MT" FontSize="24" FontWeight="Bold"
       Foreground="#FFFEE3A4"
       Content="{Binding Path=UserInformation.AccountBalance,Mode=OneWay}">
    <Label.Background>
        <ImageBrush />
    </Label.Background>
</Label>

The class whcih have the AccountBalance具有 AccountBalance 的 class

public class CustomerInformation : INotifyPropertyChanged
    {
        public CustomerInformation()
        {
            _Balance = 0.0;
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

 public double AccountBalance
        {
            get { return _Balance; } 
        set 
        {
            _wepaBalance = value;
            FirePropertyChanged("AccountBalance");
        } 

        }

 protected void FirePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
}

datacontext is set as below数据上下文设置如下

this.LayoutRoot.DataContext = this; this.LayoutRoot.DataContext = 这个;

behind the xaml.cs the following code is written to access the UserInfo which is a global object在 xaml.cs 后面编写了以下代码来访问全局 object 的 UserInfo

public CustomerInformation UserInformation
        {
            get
            {

                return Globalobjs._Object.UserInfo;
            }
        }

xamls.cs is derived from Window only. xamls.cs 仅源自 Window。

The problem is PropertyChangedEventHandler of INotifyPropertyChanged is always null when called.问题是 INotifyPropertyChanged 的 PropertyChangedEventHandler 在调用时总是 null 。

Can any 1 please help me on this issue?任何1都可以帮助我解决这个问题吗?

this.LayoutRoot.DataContext = this; 

This is the Window , yet you are setting the Window instance as the DataContext .这是Window ,但您将Window实例设置为DataContext Set the DataContext to the UserInformation .DataContext设置为UserInformation

this.LayoutRoot.DataContext = Globalobjs._Object.UserInfo;

Does the datacontext that you are binding to implement INotifyPropertyChanged?您绑定的数据上下文是否实现 INotifyPropertyChanged?

If this is not an MVVM patterned project, ensure that the class that contains the property that you are binding to implements that interface, and be sure to call the delegate for the event when you change the property (eg OnPropertyChanged("MyProperty"))如果这不是一个 MVVM 模式项目,请确保包含您要绑定到的属性的 class 实现该接口,并确保在您更改属性时调用事件的委托(例如 OnPropertyChanged("MyProperty"))

If it is an MVVM project and you are not using a framework, it is best to derive all of your ViewModels from a ViewModel base that implements INotifyPropertyChanged.如果它是一个 MVVM 项目并且您没有使用框架,那么最好从实现 INotifyPropertyChanged 的 ViewModel 基础派生所有 ViewModel。

You are binding to the Windows's DataContext.您正在绑定到 Windows 的 DataContext。 But the Windows DataContext is not the same as the Windows's code behind, where you have UserInformation property defined.但是 Windows DataContext 与 Windows 后面的代码不同,您在其中定义了 UserInformation 属性。 To access a property defined in your Window's code behind, you have to set your Window's Name property, then use the following binding instead:要访问在后面的 Window 代码中定义的属性,您必须设置 Window 的 Name 属性,然后改用以下绑定:

Content="{Binding ElementName=YourWindowName, Path=UserInformation.AccountBalance,Mode=OneWay}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM