WPF DataGrid:默认情况下取消第一行选择

时间:2010-12-01 13:52:36

标签: wpf datagrid wpftoolkit

在我的DataGrid中,默认情况下会选择第一行。我该如何取消?

<Window x:Class="WpfApplication50.Window7"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" 
    Title="Window7" SizeToContent="WidthAndHeight">
<Window.Resources>
    <!--Data-->
    <XmlDataProvider x:Key="MyData" XPath="/Info">
        <x:XData>
            <Info xmlns="">
                <Item ID="ISBN 45-F1" Name="Winner" Price="$32.05" Author="Aka" Catalog="Business"/>
                <Item ID="ISBN 54-32" Name="C++ Inside" Price="$10.00" Author="John" Catalog="Language"/>
                <Item ID="ISBN 14-A0" Name="Java Inside" Price="$9.00" Author="Tom" Catalog="Language"/>
                <Item ID="ISBN 56-78" Name="Stock Market" Price="$8.50" Author="Bob" Catalog="Business"/>
                <Item ID="ISBN AA-02" Name="Guideline for Health" Price="$19.00" Author="Lee" Catalog="Health"/>
                <Item ID="ISBN A4-07" Name="C# Inside" Price="$8.50" Author="Bob" Catalog="Language"/>
            </Info>
        </x:XData>
    </XmlDataProvider>
    <CollectionViewSource x:Key='src' Source="{Binding Source={StaticResource MyData}, XPath=Item}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="@Catalog"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
    <!--Data-->
</Window.Resources>
<Grid>
    <dg:DataGrid
     AutoGenerateColumns="False"
     Background="White" IsReadOnly="True" Padding="0" Margin="0"
     BorderThickness="0"
     GridLinesVisibility="None"
     HorizontalScrollBarVisibility="Disabled"
        HeadersVisibility="All"
     ItemsSource='{Binding Source={StaticResource src}}'
     SelectionMode="Single"    
     SelectionUnit="FullRow"
     VerticalGridLinesBrush="DarkGray"
     VerticalScrollBarVisibility="Disabled">
        <dg:DataGrid.Columns>
            <dg:DataGridTextColumn Binding="{Binding XPath=@ID}" Header="ID"/>
            <dg:DataGridTextColumn Binding="{Binding XPath=@Name}" Header="Name"/>
            <dg:DataGridTextColumn Binding="{Binding XPath=@Price}" Header="Price"/>
            <dg:DataGridTextColumn Binding="{Binding XPath=@id}" Header="Author"/>
        </dg:DataGrid.Columns>

    </dg:DataGrid>
</Grid>

1 个答案:

答案 0 :(得分:1)

xaml:


 <dg:DataGrid Name="SC_dgList"  CanUserAddRows="False" Loaded="SC_dgList_Loaded">

CS:

private void SC_dgList_Loaded(object sender, RoutedEventArgs e)
 {
      SC_dgList.SelectedIndex = -1;
 }