开发者

how to set binding for the label in wpf

开发者 https://www.devze.com 2023-03-28 01:29 出处:网络
I need to bind a property to a label. i have written the following code: xaml for the label is <Label Canvas.Left=\"807.3\" Canvas.Top=\"148.9\" Height=\"33.567\" x:Name=\"label2\"

I need to bind a property to a label. i have written the following code: xaml for the label is

<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

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;

behind the xaml.cs the following code is written to access the UserInfo which is a global object

public CustomerInformation UserInformation
        {
            get
            {

                return Globalobjs._Object.UserInfo;
            }
        }

xamls.cs is derived from Window only.

The problem is PropertyChangedEventHandler of INotifyPropertyChanged is always null when called .

Can any 1 please help me on this issue?


this.LayoutRoot.DataContext = this; 

This is the Window, yet you are setting the Window instance as the DataContext. Set the DataContext to the UserInformation.

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


Does the datacontext that you are binding to implement 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 (e.g. 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.


You are binding to the Windows's DataContext. But the Windows DataContext is not the same as the Windows's code behind, where you have UserInformation property defined. 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:

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

精彩评论

暂无评论...
验证码 换一张
取 消