2014-10-30 99 views
0

我在代码背后有一个属性文件。WPF PropertyChanged为空

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变为空常会导致不更新组合框?应该是什么解决方案?

+0

停止设置'propertyName' = NULL? – 2014-10-30 17:22:39

+1

您是否在任何时候设置了DataContext? – furkle 2014-10-30 17:23:23

+0

尝试删除组合框selectedvalue上的相对源 – DLeh 2014-10-30 17:28:34

回答

0

我相信你是有约束力的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}}"