0

我使用以下内容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");
                }
            }
        }
}

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

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

我想我在装订中遗漏了一些东西。我不明白为什么事件为空?

编辑:

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");
        }
    }
}
4

0 回答 0