首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF PropertyChanged为空

WPF PropertyChanged为空
EN

Stack Overflow用户
提问于 2014-10-31 01:18:08
回答 1查看 915关注 0票数 0

我在代码隐藏文件中有一个属性。

代码语言:javascript
复制
private int? selectedTypeID = null;
public int? SelectedTypeID
{
  get
  {
    return selectedTypeID;
  }
  set
  {
    selectedTypeID = value;
    OnPropertyChanged( "SelectedTypeID" );
  }
}

这是PropertyChanged的代码。在注释行中提到了这个问题,请参阅。

代码语言:javascript
复制
#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经常会变成空,导致不能更新组合框?解决方案应该是什么?

EN

回答 1

Stack Overflow用户

发布于 2014-10-31 01:54:07

我相信您绑定的SelectedValue是错误的。您的ComboBox本身没有任何名为SelectedTypeID的属性。它应该是视图模型的一些属性。在这种情况下,RelativeSource应该沿着可视化树向上遍历,以具有您想要的DataContext的源为目标(在这种情况下,我猜它的类型是local:EditConditionListLayer),然后Path应该以DataContext为前缀:

代码语言:javascript
复制
SelectedValue="{Binding DataContext.SelectedTypeID, 
     RelativeSource={RelativeSource AncestorType={x:Type local:EditConditionListLayer}}}"

此外,我怀疑你的ComboBox本身是否有你想要的DataContext,如果是的话,它可能是:

代码语言:javascript
复制
SelectedValue="{Binding DataContext.SelectedTypeID, 
                        RelativeSource={RelativeSource Self}}" 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26659358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档