如何从退出编辑模式停止WPF Datagridtemplatecolumn? [英] How do I stop a WPF Datagridtemplatecolumn from exiting edit mode?

查看:152
本文介绍了如何从退出编辑模式停止WPF Datagridtemplatecolumn?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个MVVM WPF应用程序,其中包含需要编辑的值的datagrid。值的含义取决于其他数据,因此我已经编写了一个用户控件,用于根据值的类型不同而不同的模板进行编辑。即它可以显示为文本框,组合框或链接到数据库以检索可能值的几个其他内部控件之一。
这是我使用的xaml。

 < DataGridTemplateColumn Header =Value> 
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< local:ValueViewingControl Value ={Binding Value}ValueType ={Binding SettingValueType}/>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< DataGridTemplateColumn.CellEditingTemplate>
< DataTemplate>
< local:ValueEditingControl Value ={Binding Value,Mode = TwoWay}ValueType ={Binding SettingValueType}/>
< / DataTemplate>
< /DataGridTemplateColumn.CellEditingTemplate>
< / DataGridTemplateColumn>

问题是,一旦编辑模式,下一次点击ValueEditingControl的任何部分会导致单元格退出编辑模式并返回到ValueViewingControl之前,我可以做任何事情。
我认为它与细胞思考失去焦点有关。有人知道一直保持单元格的编辑模式,直到我实际选择或点击单元格?




更多的测试显示我,如果我使用文本框或标准组合框作为编辑控件,它的工作原理。这使我认为它实现了我编写的自定义控件,它包含弹出窗口。当我选择作为编辑控件的一部分的弹出窗口时,它会认为我已经离开了datagridcell,所以结束了editmode。如何说服datagridcell弹出窗口是为了焦点目的?
它必须是可能的或组合框不能工作。

解决方案

感谢这个问题 WPF数据网格中的Popup焦点,我已经找出了我自己的问题的部分解决方案。

问题似乎是我的弹出窗口浮动的datagridcell尝试在我的弹出窗口中单击时抓住焦点。

我的解决方案是将此处理程序和帮助函数添加到包含弹出窗口的用户控件

  Private Sub ControlRoot_PreviewLostKeyboardFocus(sender As System.Object,e As System.Windows.Input.KeyboardFocusChangedEventArgs)
Dim popupelement As Popup = FindVisualChild(Of Popup)(ControlRoot)
If(popupelement IsNot Nothing AndAlso popupelement.IsOpen)Then
e.Handled = True
End If
End Sub

函数FindVisualChild(Of As As DependencyObject)(ByVal elemen t As DependencyObject)As T
如果元素为Nothing然后
返回没有
ElseIf TypeOf(element)是T然后
返回元素
Else
Dim count = VisualTreeHelper.GetChildrenCount(element)
对于索引As Integer = 0计数 - 1
Dim child As DependencyObject = VisualTreeHelper.GetChild(element,index)
如果TypeOf(child)是T Then
返回孩子
Else
Dim grandchild As T = FindVisualChild(Of T)(child)
如果孙子IsNot Nothing然后返回孙子
结束如果
下一个
如果
返回没有
结束功能

如果弹出窗口打开,重点离开。这不完美,所以如果任何人有更好的解决方案,我都是耳朵,但它的工作。


I'm writing an MVVM WPF app with a datagrid of values which needs to be editable. The meaning of the value varies depending on other data, so I've written a usercontrol for editing them with a template that varies depending on the type of value. i.e. it may appear as a textbox, a combobox, or one of several other inhouse controls that link to databases to retrieve possible values. This is the xaml I've used.

<DataGridTemplateColumn Header="Value">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <local:ValueViewingControl Value="{Binding Value}" ValueType="{Binding SettingValueType}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <local:ValueEditingControl Value="{Binding Value,Mode=TwoWay}" ValueType="{Binding SettingValueType}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

The problem is that once in editing mode, the next click on any part of the ValueEditingControl causes the cell to exit edit mode and go back to the ValueViewingControl before I can actually do anything. I assume its something to do with the cell thinking its lost focus. Does anyone know a way of keeping the cell in edit mode until I actually tab or click out of the cell?

[edit] A little more testing shows me that it works as expected if I use a textbox or a standard combobox as the edit control. This is making me think its the implementation of the custom controls I've written, which incorporate popups. When I select the popup that is part of the editing control, it thinks I've left the datagridcell and so ends editmode. How do I convince the datagridcell that the popup is inside it for focus purposes? It must be possible or the combobox wouldnt work.

解决方案

Thanks to this question WPF Popup focus in data grid , I've figured out a partial solution to my own problem.
The problem seems to be that the datagridcell over which my popups are floating is trying to snatch the focus when I click inside my popups.
My solution is to add this handler and helper function to the usercontrol containing the popup

Private Sub ControlRoot_PreviewLostKeyboardFocus(sender As System.Object, e As System.Windows.Input.KeyboardFocusChangedEventArgs)
    Dim popupelement As Popup = FindVisualChild(Of Popup)(ControlRoot)
    If (popupelement IsNot Nothing AndAlso popupelement.IsOpen) Then
        e.Handled = True
    End If
End Sub

Function FindVisualChild(Of T As DependencyObject)(ByVal element As DependencyObject) As T
    If element Is Nothing Then
        Return Nothing
    ElseIf TypeOf (element) Is T Then
        Return element
    Else
        Dim count = VisualTreeHelper.GetChildrenCount(element)
        For index As Integer = 0 To count - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(element, index)
            If TypeOf (child) Is T Then
                Return child
            Else
                Dim grandchild As T = FindVisualChild(Of T)(child)
                If grandchild IsNot Nothing Then Return grandchild
            End If
        Next
    End If
    Return Nothing
End Function

This stops the focus from leaving if the popup is open. It's not perfect, so if anyone has a better solution I'm all ears, but it works.

这篇关于如何从退出编辑模式停止WPF Datagridtemplatecolumn?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆