views:

267

answers:

1

I have a DataGrid that I want to simply display data in. I don't want anything to be selected. However, it always wants to select the first column of the first row.

Here is the XAML:

<myDataGrid:DataGrid 
    x:Name="grdPerson" 
    AutoGenerateColumns="False"  
    Height="478" 
    Width="302"
    IsReadOnly="True"
    RowBackground="Black"
    AlternatingRowBackground="Gray"
    GridLinesVisibility="None"
    HeadersVisibility="Column"
    Foreground="White"
    CanUserReorderColumns="False"
    CanUserResizeColumns="False"
    CanUserSortColumns="False"
    IsEnabled="False"
    AreRowDetailsFrozen="True"
    AreRowGroupHeadersFrozen="True">
    <myDataGrid:DataGrid.Columns>
        <myDataGrid:DataGridTextColumn 
            IsReadOnly="True"
            Header="Date/Time" 
            Width="125" 
            Binding="{Binding Date}" />
        <myDataGrid:DataGridTemplateColumn 
            IsReadOnly="True"
            Header="Person" 
            Width="175">
            <myDataGrid:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding FirstName}" />
                        <TextBlock Text="{Binding LastName}" />
                    </StackPanel>
                </DataTemplate>
            </myDataGrid:DataGridTemplateColumn.CellTemplate>
        </myDataGrid:DataGridTemplateColumn>
    </myDataGrid:DataGrid.Columns>
</myDataGrid:DataGrid>

and here is the code to populate the grid

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Date { get; set; }

    public Person(DateTime date, string firstName, string lastName)
    {
        Date = date;
        FirstName = firstName;
        LastName = lastName;
    }
}

// in the MainPage.xaml
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    List<Person> list = new List<Person>();
    list.Add(new Person(date: DateTime.Parse("4/14/2010 3:18 PM"), firstName: "John", lastName: "Doe"));
    list.Add(new Person(date: DateTime.Parse("4/14/2010 5:18 am"), firstName: "Jane", lastName: "Doe"));
    grdPerson.ItemsSource = list;
    grdPerson.SelectedIndex = -1;
}

When the DataGrid is displayed, this is what it looks like...

As you can see, the first cell of the first row is white because it is selected.

Any ideas how to make it not selected the first column in the first row?

+1  A: 

I've done this in the past by editing the control's templates so that the selected row looks just like an ordinary row

Rob Fonseca-Ensor