数据绑定Datagrid WPF

时间:2015-03-23 02:49:09

标签: c# wpf mvvm

我创建了一个datagrid,每个对象都有自己的行。我添加了两列(编辑,删除)但命令不会执行。我正在使用MVVM模式。这是datagrid

<DataGrid Name="dgBillMeta" ItemsSource="{Binding FileObjectCollection}" AutoGenerateColumns="False" Grid.Row="0" IsReadOnly="False" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="ID"  Binding="{Binding id}"></DataGridTextColumn>
                    <DataGridTextColumn Header="Name" Binding="{Binding attName}"></DataGridTextColumn>
                    <DataGridCheckBoxColumn Header="Key" Binding="{Binding isKey}"></DataGridCheckBoxColumn>
                    <DataGridTextColumn Header="Type" Binding="{Binding attType}"></DataGridTextColumn>
                    <DataGridTextColumn Header="Required" Binding="{Binding isRequired}"></DataGridTextColumn>
                    <DataGridTextColumn Header="Location" Binding="{Binding attLoc}"></DataGridTextColumn>
                    <DataGridTextColumn Header="Length" Binding="{Binding attLength}"></DataGridTextColumn>
                    <DataGridTextColumn Header="Decimal" Binding="{Binding isDecimal}"></DataGridTextColumn>
                    <DataGridTextColumn Header="Alignment" Binding="{Binding attAlignment}"></DataGridTextColumn>
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Edit"></Button>                                
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTemplateColumn Header="delete">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Command="{Binding remCommand}" Content="Remove"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

我可以看到编辑,删除但是当点击它时,该对象不会从我的ModelView执行。如果我把按钮放在数据网格之外(在窗口的任何地方,它确实有用)。

这里ViewModel如何工作

   internal class BillMetaDataViewModel
    {
        private ObservableCollection<BillMetaData> _MyDataList;
        private Command removeCommand;

        public BillMetaDataViewModel()
        {
            Database dbobj = new Database();
            MongoDatabase dtbase = dbobj.getDatabase;
            var collection = dtbase.GetCollection<BillMetaData>("BillMetaData");
            var entity = collection.FindAll();
            _MyDataList = new ObservableCollection<BillMetaData>(entity.ToList());
            removeCommand = new Command(removeComm);
        }

        public ObservableCollection<BillMetaData> FileObjectCollection
        {
            get { return _MyDataList; }
        }

        public void removeComm()
        {
            MessageBox.Show("Hello MessageBox"); 
        }

        public Command remCommand
        {
            get { return removeCommand; }
        }
    }

我可能缺少选择号码,但我首先想要学习我相信它必须对路径做些什么,但我不确定它是如何工作的。我在xaml上定义了我的datacontext,如下所示

InitializeComponent();
DataContext = new BillMetaDataViewModel();

1 个答案:

答案 0 :(得分:1)

按钮的DataTemplate未设置DataContext for按钮,请尝试以下

将DataGrid的名称设置为x:名称和

<DataGrid x:Name="dgBillMeta" ItemsSource="{Binding FileObjectCollection}" AutoGenerateColumns="False" Grid.Row="0" IsReadOnly="False" >

使用元素名称并设置正确的绑定路径。

<DataGridTemplateColumn Header="delete">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Command="{Binding Path=DataContext.remCommand, ElementName=dgBillMeta}" Content="Remove"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>