【问题标题】:Unable to select a certain row in a C# WPF datagrid无法选择 C# WPF 数据网格中的某一行
【发布时间】:2013-09-17 19:35:29
【问题描述】:

如上所述,我有一个数据网格 I NC# WPF,其中填充了 13 行信息。单击一行时,我可以选择该行并触发 SelectionChanged 事件,但第 10 行除外。单击此行甚至不会选择该行。如果我点击它上面或下面的行,事件就会触发,然后我可以使用键盘向上或向下箭头导航到第 10 行并选择它,然后触发事件,而不是通过鼠标单击,并且只有这一行。

行与其他行没有什么不同,并且是 100% 可重复的。那么为什么我不能用鼠标点击选择这一行呢?我应该检查什么?

<DataGrid x:Name="MyDataGrid" Width="826" Height="320" Margin="18,45,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" HorizontalContentAlignment="Center" ItemsSource="{Binding}" CanUserResizeColumns="False" IsReadOnly="True" CanUserResizeRows="False" SelectionMode="Single" AutoGenerateColumns="False" CanUserReorderColumns="False" SelectionChanged="dgBatches_SelectionChanged">  
<DataGrid.Columns>  
<DataGridTextColumn Header="Unit" Width="*" MinWidth="150" DisplayIndex="0" Binding="{Binding Path=Unit_Desc}" />  
<DataGridTextColumn Header="Current Phase" Width="Auto" MinWidth="100" DisplayIndex="1" Binding="{Binding Path=Status_Phase}" />  
<DataGridTextColumn Header="Batch" Width="Auto" MinWidth="80" DisplayIndex="2" Binding="{Binding Path=Batch_Number}" />  
</DataGrid.Columns>  
</DataGrid>  

private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)  
{  
int selectedIndex = MyDataGrid.SelectedIndex;  
}  

private void PopulateMyDataGrid()
{
StoredProcedureExecutionResult sper = null;
List<Parameter> parameterList = new List<Parameter>();
parameterList.Add(CreateServiceParameter(1, "@ProductionDay", "9/15/2013");
sper = _databaseService.ExecuteProcedure(_databaseConnectionDor, "sp_PopulateGrid", parameterList);
if (null != sper.FirstTable)
{
    DataTable batchTable = sper.FirstTable.DataTable.Copy();
    MyDataGrid.DataContext = null;
    MyDataGrid.DataContext = batchTable;
    MyDataGrid.Items.Refresh();
}
}

谢谢

【问题讨论】:

  • 老兄,贴出相关代码和XAML。
  • 在您创建行的位置发布代码。并包含数据项。
  • 行创建绑定到返回 13 行的存储过程,我也会在那里发布我正在做的事情
  • 你为什么设置DataContext而不是ItemsSource
  • 打败了我,我被交给了代码库,这就是他们正在做的事情。

标签: c# wpf datagrid


【解决方案1】:

好吧,我让它工作了。在 Visual Studio 中,我右键单击网格,转到选项,然后选择置于前面。之后,我可以单击网格中的任何行来选择它。为什么?我不知道。

【讨论】:

    猜你喜欢
    • 2011-11-20
    • 2011-01-15
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多