0

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();

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

why would this work? xName what does that do? also path datacontext? why? Will accept answer once allowed
I am trying to find official documentation on why the DataContext isn't set to the parents DataContext, (something other than this is the way it has always been) will update as soon as I find something.
Ya.. This should work, Since datacontext is BillMetaDataViewModel and your command is property of that context class only. When you trying to bind WPF is trying to find the datasource property inside the BillMetaData class which is not present hence your command not firing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.