我在代码隐藏文件中有一个属性。
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经常会变成空,导致不能更新组合框?解决方案应该是什么?
发布于 2014-10-31 01:54:07
我相信您绑定的SelectedValue是错误的。您的ComboBox本身没有任何名为SelectedTypeID的属性。它应该是视图模型的一些属性。在这种情况下,RelativeSource应该沿着可视化树向上遍历,以具有您想要的DataContext的源为目标(在这种情况下,我猜它的类型是local:EditConditionListLayer),然后Path应该以DataContext为前缀:
SelectedValue="{Binding DataContext.SelectedTypeID,
RelativeSource={RelativeSource AncestorType={x:Type local:EditConditionListLayer}}}"此外,我怀疑你的ComboBox本身是否有你想要的DataContext,如果是的话,它可能是:
SelectedValue="{Binding DataContext.SelectedTypeID,
RelativeSource={RelativeSource Self}}" https://stackoverflow.com/questions/26659358
复制相似问题