RelativeSource绑定上的PropetyChanged事件为null

时间:2013-10-10 05:49:13

标签: wpf events data-binding mvvm inotifypropertychanged

我使用以下DataGrid

<DataGrid SelectedValuePath="Key" DisplayMemberPath="Value" ItemsSource="{Binding MyModelGrid}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Width="*" Header="{x:Static p:Resources.XHeader}">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox IsEditable="True" SelectedItem="{Binding Value.X}" ItemsSource="{Binding DataContext.XList,RelativeSource={RelativeSource AncestorType=DataGrid,Mode=FindAncestor}}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

       //More details...

    </DataGrid.Columns>
</DataGrid>

DataContext是以下型号:

public class MyModel: INotifyPropertyChanged
{

        private SortedDictionary<int, GridRowObj> _myModelGrid= new SortedDictionary<int,GridRowObj>();
        public SortedDictionary<int, GridRowObj> MyModelGrid
        {
            get
            {
                return _myModelGrid;
            }
            set
            {
                if (_myModelGrid!= value)
                {
                    _myModelGrid= value;
                    OnPropertyChanged("MyModelGrid");
                }
            }
        }


        private ObservableCollection<string> _xList;
        public ObservableCollection<string> XList
        {
            get
            {
                return _xList;
            }
            set
            {
                if (_xList!= value)
                {
                    _xList= value;
                    OnPropertyChanged("XList");
                }
            }
        }
}

Dictonary - MyModelGrid初始化为20行(它们显示在屏幕上)。

当我将数据放入列表 - XList时,PropetyChangedEvent null ,因此列表显示为空。

我想我在装订中遗漏了一些东西。我无法理解为什么事件是空的?

修改

DataContext设置:

    public MyView()
    {
        InitializeComponent();
        MyViewModel vm = new MyViewModel();
        this.DataContext = vm.Model;
    }

MyViewModel.cs:

public class MyViewModel: INotifyPropertyChanged
{
  private MyModel _model = new MyModel();
    public MyModel Model
    {
        get
        {
            return _model;
        }
        set
        {
            _model= value;
            NotifyPropertyChanged("Model");
        }
    }
}

0 个答案:

没有答案