带滚动的Datagrid模板列无法编辑

时间:2015-05-19 10:10:16

标签: wpf xaml

 <DataGridTemplateColumn Width="3*" Header="Description" 

                    HeaderStyle="{StaticResource gridHeaderStyle}"

                      ScrollViewer.IsDeferredScrollingEnabled="True">

  <DataGridTemplateColumn.CellTemplate >
 <DataTemplate >
 <ScrollViewer VerticalScrollBarVisibility="Auto" Width="Auto" 
              CanContentScroll="True" Height="Auto" MaxHeight="100">

    <TextBlock Text="{Binding Description}" 

         Width="Auto" Height="Auto" TextWrapping="Wrap" 

         xml:space="preserve" />

     </ScrollViewer>

    </DataTemplate>

 </DataGridTemplateColumn.CellTemplate>

 <DataGridTemplateColumn.CellEditingTemplate>
  <DataTemplate>
     <ScrollViewer VerticalScrollBarVisibility="Auto" 
         Width="Auto" CanContentScroll="True" Height="Auto" 
         MaxHeight="100">


         <TextBox AcceptsReturn="True" Width="Auto" Height="Auto"

           Text="{Binding Description, NotifyOnTargetUpdated=True,

            Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

            TextWrapping="Wrap" xml:space="preserve" />

         </ScrollViewer>

      </DataTemplate>

    </DataGridTemplateColumn.CellEditingTemplate>

  </DataGridTemplateColumn>

我在细胞模板和细胞编辑模板周围使用滚动条

双击单元格时,它不会进入编辑模式

如果我从单元格模板中删除滚动条,则允许双击编辑

但我想在细胞模板和编辑模板

周围显示滚动条

如果按F2键进入编辑模式但鼠标双击不起作用

任何解决方案??

1 个答案:

答案 0 :(得分:0)

<DataGridTemplateColumn Width="3*" Header="Description" HeaderStyle="     {StaticResource gridHeaderStyle}"    ScrollViewer.IsDeferredScrollingEnabled="True">
                <DataGridTemplateColumn.CellTemplate >
                    <DataTemplate >
                        <ScrollViewer VerticalScrollBarVisibility="Auto" Width="Auto" CanContentScroll="True" Height="Auto" MaxHeight="100"
                                      MouseDoubleClick="ScrollViewer_MouseDoubleClick">
                            <TextBlock Text="{Binding Description}" Width="Auto" Height="Auto" TextWrapping="Wrap" xml:space="preserve" />
                        </ScrollViewer>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ScrollViewer VerticalScrollBarVisibility="Auto" Width="Auto" CanContentScroll="True" Height="Auto" MaxHeight="100">
                            <TextBox AcceptsReturn="True" Width="Auto" Height="Auto" Text="{Binding Description, NotifyOnTargetUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap" xml:space="preserve" /></ScrollViewer>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

需要在滚动查看器上添加事件

private void ScrollViewer_MouseDoubleClick(object sender,MouseButtonEventArgs e)
    {       grid.BeginEdit();     }

从msdn论坛得到答案

https://social.msdn.microsoft.com/Forums/en-US/2a59e33a-8676-4df4-ae67-f78678559a64/datagrid-editcelltemplate-with-scroll-bar-not-getting-into-edit-mode?forum=wpf