简体   繁体   中英

Data binding Datagrid WPF

I have created a datagrid with each object has their own line. I added two column (edit,remove) but the command won't execute. I am using MVVM pattern. Here is the 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>

I can see edit, and remove but when clicking on it, the object does not execute from my ModelView. If I put the button outside the datagrid ( anywhere on the windows, it does work).

Here how the ViewModel works

   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; }
        }
    }

I may be missing selection number, but i am trying to learn first I believe it has to do something with the path but i am not sure how it would work. I have defined my datacontext on the xaml as follow

InitializeComponent();
DataContext = new BillMetaDataViewModel();

The DataTemplate for the button doesn't set the DataContext for button, try the following

Set the Name of the DataGrid to x:Name and

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

use element name and set the correct binding path.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM