我正在寻求有关如何在其中一项属性发生变化时收到通知的建议。
例如,假设我想将控件的文本绑定到WPF ViewModel中的FullAddress属性,只要其中一组属性发生更改,我就可以动态计算,例如StreetNumber,StreetName,Suburb,PostCode等。
我在想我需要将我的显示控件绑定到FullAddress属性,但是如果其中一个依赖属性发生更改,我该如何让它自动更新?我可以将属性的codebehind绑定到同一ViewModel上的多个其他属性吗?如果是这样的话?还有更好的方法吗?
答案 0 :(得分:1)
假设您的类实现了INotifyPropertyChanged,那么您可以在任何属性设置器中进行通知。 E.g
#region Implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
Address.BeginEdit();
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
public string FirstLine
{
get { return firstLine; }
set
{
firstLine = value;
OnPropertyChanged("FirstLine");
OnPropertyChanged("FullAddress");
}
}
public string SecondLine
{
get { return secondLine; }
set
{
secondLine= value;
OnPropertyChanged("SecondLine");
OnPropertyChanged("FullAddress");
}
}
public string FullAddress
{
get { return firstLine + secondLine; }
}
答案 1 :(得分:1)
尝试这样的事情:
public class Address : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string address1;
public string Address1
{
get { return address1; }
set
{
address1 = value;
OnPropertyChanged("Address1");
OnPropertyChanged("FullAddress");
}
}
private string address2;
public string Address2
{
get { return address2; }
set
{
address2 = value;
OnPropertyChanged("Address2");
OnPropertyChanged("FullAddress");
}
}
private string town;
public string Town
{
get { return town; }
set
{
town = value;
OnPropertyChanged("Town");
OnPropertyChanged("FullAddress");
}
}
public string FullAddress
{
get { return string.Format("{0}, {1}, {2}", address1, address2, town); }
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
答案 2 :(得分:1)
我认为你唯一的选择是让你的类的对象订阅他们自己的INPC事件并自动更改他们的依赖属性。工作流程将如下所示:
FullAddress
的相关属性已更改,并更新FullAddress
值FullAddress
setter会触发自己的INPC事件FullAddress
已更改并更新了用户界面更新:示例代码
class NotificationExample : INotifyPropertyChanged
{
private string firstName;
private string lastName;
public event PropertyChangedEventHandler PropertyChanged;
public string FirstName
{
get { return this.firstName; }
set
{
this.firstName = value;
this.OnPropertyChanged("FirstName");
}
}
public string LastName
{
get { return this.lastName; }
set
{
this.lastName = value;
this.OnPropertyChanged("LastName");
}
}
public string FullName
{
get { return string.Format("{0} {1}", this.firstName, this.lastName); }
}
public NotificationExample()
{
this.PropertyChanged += this.NotifyIfFullNameChanged;
}
private void OnPropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private void NotifyIfFullNameChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "FirstName" || e.PropertyName == "LastName")
{
this.OnPropertyChanged("FullName");
}
}
}
答案 3 :(得分:1)
您可以使用MultiBinding并且根本不使用FullAddress属性
的修改:
如果您只需显示完整地址,则可以使用StringFormat
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1}">
<Binding Path="City"/>
<Binding Path="Street"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
如果您希望允许用户编辑整个地址字符串然后将其拆分为部分,那么您需要实现IMultiValueConverter接口