2010-12-07 52 views
0

当我有第二个数据网格作为rowdetailstemplate时,我在我的数据网格中遇到了一些奇怪的行为。主数据网格绑定到我的项目集合,并且详细信息数据网格绑定到项目包含的子项目集合。现在,所有这些都完美呈现,但是当我想直接单击SubItemsGrid中的单元格时,它首先选择包含SubItemsGrid的主网格中该行的第一个单元格。我必须再次单击才能到达要选择的单元格。选择RowDetailsTemplate中的WPF DataGrid的单元格

有没有人有过这样的想法?如果是这样,是否有解决方法?

这是我的加价(部分):

<DataGrid x:Name="ItemGrid" ItemsSource="{Binding Path=Items}" AutoGenerateColumns="False" SelectionUnit="Cell" 
      RowDetailsVisibilityMode="Visible" CanUserResizeRows="False" AreRowDetailsFrozen="False" VerticalAlignment="Top" 
      CanUserAddRows="False" CanUserDeleteRows="False" VerticalScrollBarVisibility="Hidden"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="Column1" Binding="{Binding Path=ID}" Width="350"/> 
     <DataGridTextColumn Header="Column2" Binding="{Binding Path=Name}" Width="80"/> 
     <DataGridTextColumn Header="Column3" Binding="{Binding Path=Description}" Width="80"/> 
    </DataGrid.Columns> 
    <DataGrid.RowDetailsTemplate> 
     <DataTemplate> 
      <DataGrid x:Name="SubItemsGrid" ItemsSource="{Binding Path=SubItems}" AutoGenerateColumns="False" 
         SelectionUnit="Cell" HeadersVisibility="None" Margin="50,0,0,0" VerticalAlignment="Top" CanUserAddRows="False" 
         CanUserResizeRows="False" CanUserDeleteRows="False" BorderThickness="0"> 
       <DataGrid.Columns> 
        <DataGridTextColumn Header="Column1" Binding="{Binding Path=Name}" Width="300" /> 
        <DataGridTextColumn Header="Column2" Binding="{Binding Path=Description}" Width="80"/> 
        <!-- Etc.--> 

---编辑---

好吧,我想出了主意,关于办理SubItemsGrid鼠标向上事件,然后将焦点设置到SubItemsGrid在代码中,像这样的:

private void SubItemsGrid_MouseUp(object sender, MouseButtonEventArgs e) 
{ 
    DataGrid selectedGrid = sender as DataGrid; 
    if (selectedGrid != null) 
    { 
     selectedGrid.Focus() 
    } 
} 

调试表明,“焦点”方法被调用右边格,但我没有得到任何的视觉效果。然而,我感觉我非常接近解决方案。任何人?

回答

0

我通过捕获SubItemsGrid的'SelectedCellsChanged'事件来解决这个问题。在处理程序中,我在调用事件的网格上调用了“BeginEdit()”。这确实将焦点直接放在了单击的单元上,而且也将单元置于编辑模式。这就是为什么我之后直接调用CancelEdit()。这将把重点放在单元格上,而不是在编辑模式下。

private void SubItemsGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) 
{ 
    DataGrid selectedGrid = sender as DataGrid; 
    if (selectedGrid != null) 
    { 
     selectedGrid.BeginEdit(); 
     selectedGrid.CancelEdit(); 
    } 
} 
+0

第一次点击的吞咽是我见过自己,虽然没有与子网格。它也在[这sliverlight.net线程](http://forums.silverlight.net/forums/p/208961/491280.aspx)中评论。我还没有想出一个通用的解决方案。 – 2011-04-21 15:20:04

相关问题