【问题标题】:Hide the DataGrid while TextBox Loses its Focus If DataGrid is not Focused如果 DataGrid 未获得焦点,则在 TextBox 失去焦点时隐藏 DataGrid
【发布时间】:2013-05-30 20:23:16
【问题描述】:

我有一个文本框和 DataGrid

当文本框失去焦点时,如果 DataGrid 不是 Focused,那么我想隐藏 DataGrid。

我使用下面的代码。

Private Sub txt_LostFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles txt.LostFocus

    If DataGrid1.IsFocused = False Then
        DataGrid1.Visibility = Windows.Visibility.Hidden
    End If

End Sub

即使我点击 DataGrid 上的任何项,DataGrid 也隐藏了。

我的代码有问题吗?

【问题讨论】:

    标签: wpf vb.net datagrid textbox focus


    【解决方案1】:

    我不确定问题是什么...您描述的行为与您的代码一致。

    行为可能与您的预期不同... 我认为当 TextBox 失去焦点时,DataGrid 将永远不会获得焦点,因为 TextBox 没有完成失去焦点。是这个问题吗?

    如果这是问题所在,您可以在隐藏 DataGrid 之前添加某种延迟(当然是以非阻塞方式)。您可以创建一个新线程,在隐藏控件之前在该线程上执行 Sleep(500),然后看看会发生什么。 您还需要小心,因为只有 UI 线程可能会更改可见控件,但如果您选择这样做,您可能会寻求进一步的帮助。

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      当文本框失去焦点甚至被触发时.. gridview 还没有聚焦..

      所以,添加类似这样的内容

      Dim lDGVFocused as Boolean
      
      Private Sub Datagrid1_Enter( ... ) ...
      
        lDGVFocused = True
      
      End Sub
      
      Private Sub Datagrid1_LostFocus( ... ) ...
      
        lDGVFocused = False
      
      End Sub
      
      Private Sub txt_LostFocus( ... ) ...
      
          If not lDGVFocused then DataGrid1.Visible = False
      
      End Sub
      
      Private Sub txt_GotFocus( ... ) ...
      
         DataGrid1.Visible = True
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2013-04-03
        • 1970-01-01
        • 1970-01-01
        • 2012-03-31
        • 1970-01-01
        • 2012-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多