0

I have this View Model:

public class Person
{
    public bool Selected;
    public string Name;  
    public bool IsMaried;
    public DataTime bDay;
}

List<Person> col;

And this is my DataGrid:

<DataGrid
    Grid.Row="1"
    Name="dataGrid"
    ItemsSource="{Binding col}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Width="60" Header="Select" SortMemberPath="IsSelected">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel>
                        <CheckBox  IsChecked="{Binding Selected}" />
                        <Image/>
                    </StackPanel>

                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <DataGridTextColumn 
            Header="Name"
            Binding="{Binding Name}" 
            Width="180"/>
    </DataGrid.Columns>
</DataGrid>

I have several issues:

  1. In order to configure columns Width I added this columns inside <DataGrid.Columns> and now i can see my Data Grid columns but if I add this manually every column appears twice.

  2. In my Person class I have another member - DataTime that I don't want to see inside my DataGrid but add Context Menu click and then show this Value. How can I remove it from my DataGrid ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mark yer
  • 403
  • 6
  • 12

1 Answers1

0

You need to set the AutoGenerateColumns="False" and add all the columns that you want to display manually. So your DataGrid could be something like this :

<DataGrid Grid.Row="1"
            Name="dataGrid"
            ItemsSource="{Binding col}"
            AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
        <DataGridCheckBoxColumn Header="Selected" Binding="{Binding Selected}"/>
        <DataGridCheckBoxColumn Header="IsMaried" Binding="{Binding IsMaried}"/>
    </DataGrid.Columns>
</DataGrid> 
MRebai
  • 5,344
  • 3
  • 33
  • 52