2017-02-21 88 views
1

我想將可編輯文本剪輯控件放入Listview數據模板中。我遵循this article,它運作良好。WPF自定義控件在預覽列表視圖中無法正常工作

但是,當我把這個控件放在一個Listview數據模板中,雙擊文本塊時,自定義控件的事件OnMouseDoubleClick被觸發,但文本框從不顯示。

我的DataTemplate:

<DataTemplate x:Key="ItemTemplate"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="auto" /> 
     </Grid.ColumnDefinitions> 

     <StackPanel Orientation="Horizontal" 
          Grid.Column="0"> 
      <Image Source="{Binding Icon}" 
          Margin="0 0 4 0" /> 
      <localp:EditableTextBlock Text="{Binding Tag, Mode=TwoWay}" 
           VerticalAlignment="Center" /> 
     </StackPanel> 
    </Grid> 

<ListView 
    ItemTemplate={StaticResource ItemTemplate} 
    .... /> 

而且我不知道爲什麼OnMouseDoubleClick EditableTextBlock被激發,但如預期的那樣從不顯示內部文本框。

感謝是您的幫助,

問候

回答

1

變化從零到別的東西的TextBlockForegroundColorPropertyTextBoxForegroundColorProperty默認值:

public static readonly DependencyProperty TextBlockForegroundColorProperty = 
    DependencyProperty.Register("TextBlockForegroundColor", 
    typeof(Brush), typeof(EditableTextBlock), new UIPropertyMetadata(Brushes.Black)); 

public static readonly DependencyProperty TextBoxForegroundColorProperty = 
    DependencyProperty.Register("TextBoxForegroundColor", 
    typeof(Brush), typeof(EditableTextBlock), new UIPropertyMetadata(Brushes.Black)); 

或者將它們設置在你的XAML:

<local:EditableTextBlock TextBlockForegroundColor="Black" TextBoxForegroundColor="Black" ... /> 

編輯

可以鍵盤焦點設置到文本框,但是,你應該設置e.Handled爲true,或OnTextBoxLostFocus將執行並隱藏你的TextBox。

protected override void OnMouseDoubleClick(MouseButtonEventArgs e) 
    { 
     base.OnMouseDoubleClick(e); 
     this.m_TextBlockDisplayText.Visibility = Visibility.Hidden; 
     this.m_TextBoxEditText.Visibility = Visibility.Visible; 
     if (m_TextBoxEditText.IsKeyboardFocusWithin ==false) 
     { 
      Keyboard.Focus(m_TextBoxEditText); 
      e.Handled = true; 
      m_TextBoxEditText.CaretIndex = m_TextBoxEditText.Text.Length; 
     } 
    } 
+0

是的,我這樣做之前,(對不起,如果我沒有帶指定的話),因爲它是透明的。但問題是一樣的:在列表視圖中,當我雙擊EditableTextBlock時,文本框從不顯示。謝謝。 – ArthurCPPCLI

+0

看到我的編輯。如果你沒有改變提供的鏈接中的代碼,這是有效的。 – Ron

+0

噢,很好,它的工作原理。非常感謝 :) – ArthurCPPCLI

相關問題