0

This is my DataGrid;

    <DataGrid Visibility="Visible"
        Grid.Row="1"
        SelectionUnit="CellOrRowHeader"
        Name="dataGrid"
        SelectionMode="Single"
        ItemsSource="{Binding collcection}">
        <DataGrid.ContextMenu>
            <ContextMenu>
                <MenuItem Command="Copy" Click="MenuItem_Click_1"/>
            </ContextMenu>
        </DataGrid.ContextMenu>
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Select">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox 
                            x:Name="cbRunRobot"
                            IsChecked="{Binding Value}"
                            Width="60"
                            Height="25"
                            Checked="cbRunRobot_Checked"
                            Unchecked="cbRunRobot_Unchecked"
                            Margin="25,0,0,0" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>
    </DataGrid>

View Model:

list<MyData> collcection;
public class MyData
{
   public string Name;
   public string Id;
}

I try to add this Column:

<DataGridColumn Binding="{Binding Name}" Header="Name" Width="180"/>

And got this errors:

Error 1 The type "DataGridColumn" is abstract and must include an explicit value.

Error 2 The member "Binding" is not recognized or is not accessible.

mark yer
  • 403
  • 6
  • 12

2 Answers2

1

DataGridColumn is an abstract class which means it cannot be instantiated. The same applies to DataGridBoundColumn.

You're choices are:

  • DataGridCheckBoxColumn for boolean values
  • DataGridComboBoxColumn for enumerable values
  • DataGridHyperlinkColumn for Uri values
  • DataGridTemplateColumn to show any types of data by defining your own cell template
  • DataGridTextColumn to show text values

It looks like DataGridTextColumn is what you're looking for.

KornMuffin
  • 2,887
  • 3
  • 32
  • 48
0

Hi I can suggest you the next:

  1. Bind to ObservableCollection instead the list.
  2. Make your MyData model to implement InotifyPropertyChanged.
  3. Make each binding involved property in MyData model to fire OnPropertyChanged event.
  4. Here is the link to the working example:How to Display and select items in a Datagrid ComboBox with WPF C#, using MVVM.

regards,

Community
  • 1
  • 1
Ilan
  • 2,762
  • 1
  • 13
  • 24