如何在Silverlight 4中制作DataGrid显示没有选择?

时间:2010-04-19 14:32:56

标签: c# silverlight silverlight-4.0 silverlight-toolkit

我有一个DataGrid,我想简单地显示数据。我不想要任何东西被选中。但是,它总是希望选择第一行的第一列。

这是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>

以下是填充网格的代码

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

显示DataGrid时,这就是它的样子...... http://img405.yfrog.com/img405/5534/4192010102546am.png

如您所见,第一行的第一个单元格是白色的,因为它已被选中。

如何让它没有被选中第一行的第一列?

1 个答案:

答案 0 :(得分:2)

我过去通过编辑控件的模板来完成此操作,以便所选行看起来就像普通行