为什么我不能在WPF数据网格中选择单元格?

本文关键字:网格 选择 单元格 数据网 数据 不能 WPF 为什么 | 更新日期: 2023-09-27 18:09:06

我有一个WPF数据网格,看起来像这样:

<DataGrid x:Name="dataGridOrderItems" Margin="369,0,4,51" VerticalAlignment="Bottom" Height="405" Grid.Row="1"
    VerticalGridLinesBrush="LightGray"  HorizontalGridLinesBrush="LightGray" AlternatingRowBackground="Beige"  AlternationCount="2" SelectionMode="Single" 
    SelectionChanged="dataGridOutstandingOrders_SelectionChanged" AutoGenerateColumns="False" IsReadOnly="True" Grid.ColumnSpan="2">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Resource Name" Binding="{Binding ResourceName}" />
        <DataGridTextColumn Header="Quantity Ordered" Binding="{Binding Quantity}" />
        <DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate, StringFormat='{0:d'}}" />
        <DataGridCheckBoxColumn Header="Dispatched Resource" Binding="{Binding IsChecked}" />
        <DataGridTextColumn/>
    </DataGrid.Columns>
</DataGrid>

即使启用了数据网格,并且指定了选择模式,我也不能单击进入任何单元格。我还错过了什么?

如果它是相关的,这里是完整的XAML窗口:

<Window x:Class="OrderProcessor.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:OrderProcessor"
        mc:Ignorable="d"
        Title="Order Processing" Height="727.625" Width="1088" Icon="1438333970_Delivery.ico">
    <Grid Margin="0,0,2,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="9*"/>
            <ColumnDefinition Width="14*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="94*"/>
            <RowDefinition Height="315*"/>
        </Grid.RowDefinitions>
        <Button x:Name="buttonRefreshOrders" Content="Load/Refresh Orders List" Margin="0,36,10,0" RenderTransformOrigin="-0.72,-2.65" ToolTip="Click to refresh the orders list with the latest orders from SharePoint" Grid.Column="1" HorizontalAlignment="Right" Width="140" Height="36" VerticalAlignment="Top" Click="buttonRefreshOrders_Click"/>
        <DataGrid x:Name="dataGridOutstandingOrders" Margin="11,7,156,470" RenderTransformOrigin="0.5,0.5" Grid.RowSpan="2" Grid.ColumnSpan="2" VerticalGridLinesBrush="LightGray"  HorizontalGridLinesBrush="LightGray" AlternatingRowBackground="Beige"  AlternationCount="2" SelectionMode="Single" SelectionUnit="FullRow" SelectionChanged="dataGridOutstandingOrders_SelectionChanged" AutoGenerateColumns="False" IsReadOnly="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Persona" Binding="{Binding Persona }" />
                <DataGridTextColumn Header="Customer Name" Binding="{Binding CustomerName }" />
                <DataGridTextColumn Header="Organization" Binding="{Binding Organization }" />
                <DataGridTextColumn Header="E-mail" Binding="{Binding Email }" />
                <DataGridTextColumn Header="Phone" Binding="{Binding PhoneNumber }" />
                <DataGridTextColumn Header="Street" Binding="{Binding Street }" />
                <DataGridTextColumn Header="Suburb" Binding="{Binding Suburb }" />
                <DataGridTextColumn Header="Postcode" Binding="{Binding Postcode }" />
                <DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate , StringFormat={}{0:d}}" />
            </DataGrid.Columns>
        </DataGrid>
        <Button x:Name="button_Dispatch_Current_Item" Content="Set selected resources to be dispatched" Margin="0,0,10,10" RenderTransformOrigin="-0.72,-2.65" ToolTip="" HorizontalAlignment="Right" Width="462" Grid.Row="1" Grid.Column="1" Height="36" VerticalAlignment="Bottom"/>
        <GroupBox x:Name="groupBoxCustomerDetails" Header="Customer Details" Margin="11,0,0,10" Grid.Row="1"
                  Grid.Column="0" VerticalAlignment="Bottom" Height="455" HorizontalAlignment="Left" Width="353">
            <StackPanel>
                <Label x:Name="labelCustomerName" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="16" FontWeight="Bold"/>
                <Label x:Name="labelCustomerOrganization" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="16" />
                <Label x:Name="labelCustomerStreet" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="16" />
                <Label x:Name="labelCustomerSuburb" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="16" />
                <Label x:Name="labelCustomerPostcode" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="16" />
            </StackPanel>
        </GroupBox>
        <DataGrid x:Name="dataGridOrderItems" Margin="369,0,4,51" VerticalAlignment="Bottom" Height="405" RenderTransformOrigin="0.5,0.5" Grid.Row="1"
            VerticalGridLinesBrush="LightGray"  HorizontalGridLinesBrush="LightGray" AlternatingRowBackground="Beige"  AlternationCount="2" SelectionMode="Single" 
            SelectionChanged="dataGridOutstandingOrders_SelectionChanged" AutoGenerateColumns="False" IsReadOnly="True" Grid.ColumnSpan="2">
            <DataGrid.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform AngleY="-0.146"/>
                    <RotateTransform/>
                    <TranslateTransform Y="-0.497"/>
                </TransformGroup>
            </DataGrid.RenderTransform>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Resource Name" Binding="{Binding ResourceName}" />
                <DataGridTextColumn Header="Quantity Ordered" Binding="{Binding Quantity}" />
                <DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate, StringFormat='{0:d'}}" />
                <DataGridCheckBoxColumn Header="Dispatched Resource" Binding="{Binding IsChecked}" />
                <DataGridTextColumn/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

为什么我不能在WPF数据网格中选择单元格?

最后我自己解决了这个问题——看起来我没有同时使用两个选择模式。更新(工作XAML):

<DataGrid x:Name="dataGridOrderItems" Margin="369,0,4,51" VerticalAlignment="Bottom" Height="405" Grid.Row="1"
VerticalGridLinesBrush="LightGray"  HorizontalGridLinesBrush="LightGray" AlternatingRowBackground="Beige"  AlternationCount="2" 
SelectionMode="Single" SelectionUnit="FullRow" 
AutoGenerateColumns="False" IsReadOnly="False" Grid.ColumnSpan="2">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Resource Name" Binding="{Binding ResourceName}" />
        <DataGridTextColumn Header="Quantity Ordered" Binding="{Binding Quantity}" />
        <DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate, StringFormat='{0:d'}}" />
        <DataGridCheckBoxColumn Header="Dispatched Resource" Binding="{Binding IsChecked}" />
        <DataGridTextColumn/>
    </DataGrid.Columns>
</DataGrid>

我认为你的isReadOnly属性导致了这个。当你把isReadOnly改为False来解决这个问题后