2014-10-30 2 views
0

파일의 코드에 속성이 있습니다.WPF PropertyChanged가 null입니다.

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이되어 콤보 상자가 업데이트되지 않는 이유는 무엇입니까? 해결책은 무엇이되어야합니까?

+0

정지'propertyName' = null을 설정 : 그래서 경우가 될 수있다, 심지어 ComboBox 자체가 원하는 DataContext을 가지고 의심? –

+1

언제든지 DataContext를 설정 했습니까? – furkle

+0

combobox selectedvalue에서 상대 소스를 삭제 해보세요. – DLeh

답변

0

나는 당신이 SelectedValue을 구속하고 있다고 생각합니다. ComboBox에는 SelectedTypeID이라는 속성이 없습니다. 뷰 모델의 일부 속성이어야합니다. 또한

SelectedValue="{Binding DataContext.SelectedTypeID, 
    RelativeSource={RelativeSource AncestorType={x:Type local:EditConditionListLayer}}}" 

:이 경우 RelativeSource (이 경우에 나는 그것이 local:EditConditionListLayer의 유형이 추측) 당신이 원하는 어떤 DataContext을 가진 일부 소스를 대상으로 시각적 트리를 걸어 만일, Path는 접두사로 DataContext을 가져야한다

SelectedValue="{Binding DataContext.SelectedTypeID, 
         RelativeSource={RelativeSource Self}}" 
관련 문제