2009-11-23 74 views
4

我正在創建一個自定義UserControl以便在DataGrid編輯模板中使用。 它看起來像這樣:WPF Popup專注於數據網格

<UserControl 
    x:Class="HR.Controls.UserPicker" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <Grid> 
     <TextBlock x:Name="PART_TextBox" Text="Hello WOrld" /> 
     <Popup Width="234" Height="175" IsOpen="True" StaysOpen="True" 

      Placement="Bottom" 
      PlacementTarget="{Binding ElementName=PART_TextBox}" 
     > 
      <TextBox 
        x:Name="searchTextBox" 
        Text="&gt;Enter Name&lt;"/> 
     </Popup> 
    </Grid> 
</UserControl> 

編輯: 我已經縮小的代碼位。 看來,如果我直接在CellEditingTemplate中放置一個帶有文本框的彈出窗口,文本框就不會出現問題。當我將該代碼移動到UserControl中時,我無法在編輯單元格時選擇該文本框。

用戶控件是否做了一些有趣的焦點?


問題是,當我編輯在DataGrid中,我得到了呈現在用戶控制的細胞,但我不能在文本框中searchTextBox點擊。當我點擊它時,彈出窗口關閉,單元格恢復爲默認值。

我已經嘗試複製並粘貼用戶控件中的所有代碼,並將其直接粘貼到CellEditingTemplate中,並以它應該的方式進行交互。

我只是想知道如果UserControl做了一些奇怪的事情,防止彈出窗口獲得焦點,因爲它直接放置在CellEditingTemplate中時按預期工作?

謝謝,勞爾

+1

那麼該用戶控件有一些奇怪的與它不把一個彈出的重點是其自身的一部分。所以我最終不得不創建一個自定義控件並抓取UserControl。這是一種痛苦,但值得您獲得自由。 – HaxElit 2009-12-04 19:33:53

回答

2

我有一個有點simular問題,我創建了一個包含一個文本框,一個按鈕和一個日曆用戶控件。基本上我用自定義驗證邏輯創建我自己的日期選擇器。

我把這個組件放在CellEditingTemplate中。當我按下按鈕時,彈出窗口顯示,但單擊任何地方的彈出窗口都會導致單元停止編輯(因爲彈出窗口正在從文本框中獲取焦點)。我通過添加代碼來解決這個問題,即如果彈出窗口打開,文本框的焦點不會丟失。這爲我做了詭計。

另外,在用戶控件的加載事件處理程序我給焦點的文本框。 在你的情況下,它是適當的Usercontrol itsefl有焦點。

protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e) { 
      // Don't allow focus to leave the textbox if the popup is open 
      if (Popup.IsOpen) e.Handled = true; 
} 

private void Root_Loaded(object sender, RoutedEventArgs e) { 
     TextBox.Focus(); 
} 
3

我有一個地方嵌入PopupUserControl作爲一個單元格編輯模板時點擊了它的某些領域將關閉類似的問題。問題原來是,WPF工具包(可能是WPF4)DataGrid用鼠標左鍵點擊非常貪婪。即使您處理它們並將Handled設置爲true,網格也可以將它們解釋爲單擊到不同的單元格中。

thread有充分的細節,但解決方法是掛接到DataGrid.CellEditEnding事件並取消年底編輯:

private static void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
{ 
    if (e.Column.GetType() == typeof(DataGridTemplateColumn)) 
    { 
     var popup = GetVisualChild<Popup>(e.EditingElement); 
     if (popup != null && popup.IsOpen) 
     { 
      e.Cancel = true; 
     } 
    } 
} 

private static T GetVisualChild<T>(DependencyObject visual) 
    where T : DependencyObject 
{ 
    if (visual == null) 
     return null; 

    var count = VisualTreeHelper.GetChildrenCount(visual); 
    for (int i = 0; i < count; i++) 
    { 
     var child = VisualTreeHelper.GetChild(visual, i); 

     var childOfTypeT = child as T ?? GetVisualChild<T>(child); 
     if (childOfTypeT != null) 
      return childOfTypeT; 
    } 

    return null; 
} 

完全學分制這轉到Actipro thread

+0

工程就像一個魅力! – JanW 2014-08-14 08:01:25

3

不知道這是否會幫助任何人,但這有助於如果你在彈出的數據網格中有自定義控件.....這解決了我的問題,它是一行xaml。我花了整整一天的時間重新閱讀本論壇,然後查看DataGridCell的源代碼。希望這可以幫助。

<Style TargetType="{x:Type DataGridCell}"> 
     <Setter Property="Focusable" Value="False"></Setter> 
    </Style> 
+1

這是在DataGridCell中彈出一個可能的最簡單的工作解決方案。 – Abbas 2017-01-12 11:44:50