【问题标题】:GridView is not adding rows to collectionGridView 没有向集合添加行
【发布时间】:2013-04-08 16:46:41
【问题描述】:

我正在开发基于 MVVM 的 WPF 应用程序。 我需要创建带有 2 个 ComboBox 列的 DataGrid。

我创建了下一个网格:

 <DataGrid Grid.Column="1" Grid.Row="4" AutoGenerateColumns="False" Margin="0,8,20,8" CanUserAddRows="True" CanUserDeleteRows="True"  ItemsSource="{Binding MapsGrid}">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Main Category" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox
                ItemsSource="{Binding DataContext.MainCategories, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                DisplayMemberPath="Category"
                                SelectedItem="{Binding DataContext.MainCategorySelectedItem, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Sub Category" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox
                ItemsSource="{Binding DataContext.SubCategories, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                DisplayMemberPath="Category"
                                  SelectedItem="{Binding DataContext.SubCategorySelectedItem, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                                />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>

        </DataGrid>

网格看起来完全符合我的需要,并且 ComboBox 控件包含数据,但我不知道为什么网格不向我的集合插入新行。

在我的视图模型中,我有下一个集合:

     private ObservableCollection<MapsDescGridModel> _mapsGrid;
            public ObservableCollection<MapsDescGridModel> MapsGrid
            {
                get { return _mapsGrid; }
                set
                {
                    if (Equals(value, _mapsGrid)) return;
                    _mapsGrid = value;
                    RaisePropertyChanged("MapsGrid");
                }
            }

我在构造函数中初始化它,我在数据网格中看到一个空白行,但我无法添加行(我正在尝试使用 Enter 键)

对象“MapsDescGridModel”包含 2 个实体(实体框架实体)

 public class MapsDescGridModel: NotificationObject
    {
        public MapsDescGridModel()
        {

        }

        public MapsDescGridModel(MainCategories mainCat, SubCategories subcat)
        {
            MainCategory = mainCat;
            SubCatergory = subcat;
        }

        private MainCategories _mainCategory;
        public MainCategories MainCategory
        {
            get { return _mainCategory; }
            set
            {
                if (Equals(value, _mainCategory)) return;
                _mainCategory = value;
                RaisePropertyChanged("MainCategory");
            }
        }


        private SubCategories _subCatergory;
        public SubCategories SubCatergory
        {
            get { return _subCatergory; }
            set
            {
                if (Equals(value, _subCatergory)) return;
                _subCatergory = value;
                RaisePropertyChanged("SubCatergory");
            }
        }

    }
}

我尝试通过代码添加列,但我只能看到一行(其余的都是该行的副本)。

可能是什么问题?

【问题讨论】:

    标签: wpf mvvm datagrid


    【解决方案1】:

    DataGridColumns 不会被添加到可视化树中。这就是为什么使用 vt 的绑定(例如 ElementName、FindAncestor 绑定)在那里不起作用的原因。 这个问题看起来类似于this one。

    顺便说一句:如果有很多行但只有一个 DataContext,那么将每一行的 SelectedItem 绑定到主 DataContext 有什么意义?您应该将每行的 SelectedItem 绑定到 MapsDescGridModel 类中的属性。

    【讨论】:

    • 是的,我看到我的逻辑是错误的。我需要考虑新的设计。感谢您的澄清
    猜你喜欢
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多