简体   繁体   中英

Silverlight.Binding issue

I have a view model with a property defined as:

private SomeEntity currentEntity;
public SomeEntity CurrentEntity
{
    get { return currentEntity; }
    set { currentEntity = value; RaisePropertyChanged(() => CurrentEntity); }
}

In a view I defined a binding to SomeEntity.Name property:

<TextBlock Text="{Binding CurrentEntity.Name }" />

This works. But when I wrote this in another way this doesn't work, CurrentEntity is always null:

public string Name
{
    get
    {                    
        return CurrentEntity != null ? CurrentEntity.Name : null;
    }
    set
    {
        CurrentEntity.Name = value;
        RaisePropertyChanged(() => CurrentEntity);
    }
}

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

Do you have any ideas why? Is there any workaround?

Use RaisePropertyChanged( () => Name)

Internally, the binding subscribes to the PropertyChanged event of INotifyPropertyChanged . When binding to the "Name" property, it will check if the Property parameter of the event arguments equals the name of the property it binds to (ie: PropertyChangedEventArgs.PropertyName == "Name"). If not it will ignore the notification.

我认为您必须为Name创建属性并为Name添加到RaisePropertyChange

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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