DocField
有一个公共布尔属性IsSelected
DocField
实现了INotifyPropertyChanged
我需要在UserControlDocFieldBaseB
更改DocField.IsSelected
时触发的事件。
我该怎么做?
public partial class UserControlDocFieldBaseB : UserControl
{
private DocField docField = null;
public UserControlDocFieldBaseB(DocField DocField)
{
InitializeComponent();
docField = DocField;
}
基于dkozl的评论,这是我如何连接它的 UserControlDocFieldString相当昂贵,所以我只想按需加载它
我怀疑我可以使用Loaded事件而不是UserControl1_DataContextChanged,但这似乎工作了 它也适用于Loaded事件
public UserControlDocFieldBaseB()
{
InitializeComponent();
DataContextChanged += new DependencyPropertyChangedEventHandler(UserControl1_DataContextChanged);
}
void UserControl1_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender is UserControlDocFieldBaseB)
{
UserControlDocFieldBaseB uc = (UserControlDocFieldBaseB)sender;
if (uc.DataContext is DocFieldString)
{
if (docFieldString == null)
{
docFieldString = (DocFieldString)uc.DataContext;
docFieldString.PropertyChanged += DocFieldString_PropertyChanged;
}
}
}
}
void DocFieldString_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "IsSelected":
//MessageBox.Show(docFieldString.Name + " docFieldString.IsSelected " + docFieldString.IsSelected.ToString());
if (userControlDocFieldString == null && docFieldString != null && docFieldString.IsSelected)
{
userControlDocFieldString = new UserControlDocFieldString(docFieldString);
this.frmEdit.Content = userControlDocFieldString;
}
break;
}
}
答案 0 :(得分:1)
由于DocField
为INotifyPropertyChanged
属性实现IsSelected
,您只需订阅PropertyChanged
DocField
事件,例如加载控件或{{1}如果属性为'IsSelected属性
DocField