我想在Listview datatemplate中放置一个editabletextblock cutom控件。我跟随this article并且效果很好。
但是当我将此控件放在Listview数据窗口中时,双击Textblock,会触发自定义控件的事件OnMouseDoubleClick,但文本框永远不会显示。
我的数据模板:
<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,但内部文本框永远不会按预期显示。
谢谢你的帮助,
此致
答案 0 :(得分:1)
将TextBlockForegroundColorProperty
和TextBoxForegroundColorProperty
的默认值从null更改为其他内容:
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" ... />
修改强>
您可以将键盘焦点设置为TextBox,但是,您应该将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;
}
}