【发布时间】:2014-10-30 17:18:08
【问题描述】:
我在代码隐藏文件中有一个属性。
private int? selectedTypeID = null;
public int? SelectedTypeID
{
get
{
return selectedTypeID;
}
set
{
selectedTypeID = value;
OnPropertyChanged( "SelectedTypeID" );
}
}
这是 PropertyChanged 的代码。问题在注释行中提到,请看。
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged( string propertyName )
{
/*PropertyChanged appears to be null in some cases while in some cases it is not null. I have also tried to explicity assigning it the DataContext but that does not work as well. */
if( PropertyChanged != null )
PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
}
#endregion
//This line is in DataContext file where the problematic property is assigned a null.
editLayerSidebar.editConditionIngredient.SelectedTypeID = null;
//This is the combobox xaml where SelectedTypeID has been bound to SelectedValue.
<ComboBox x:Name="TypeCombo" Grid.Row="3" Grid.Column="1" Margin="5,5,0,0"
ItemsSource="{Binding DataContext.IngredientTypes, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:EditConditionListLayer}}}"
DisplayMemberPath="Name" SelectedValuePath="ID" SelectedValue="{Binding SelectedTypeID, RelativeSource={RelativeSource Mode=Self}}" >
为什么 ProperyChanged 变为 null 经常导致不更新组合框?应该怎么解决?
【问题讨论】:
-
停止设置
propertyName= null? -
你有没有在任何时候设置过DataContext?
-
尝试删除组合框 selectedvalue 上的相对源
标签: wpf propertychanged