2017-02-21 89 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

相关问题