问题描述
使用 WPF,我有一个 ListBox 控件,里面有一个 DataTemplate.相关的 XAML 代码如下所示:
Using WPF, I have a ListBox control with a DataTemplate inside it. The relevant XAML code is shown below:
<ListBox Name="_todoList" Grid.Row="1" BorderThickness="2"
Drop="todoList_Drop" AllowDrop="True"
HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
AlternationCount="2">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" Checked="CheckBox_Check" />
<TextBlock Name="descriptionBlock"
Grid.Column="1"
Text="{Binding Description}"
Cursor="Hand" FontSize="14"
ToolTip="{Binding Description}"
MouseDown="TextBlock_MouseDown" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我想要做的是让 TextBlock 响应(双击)单击,将其变成 TextBox.然后,用户可以编辑描述,然后按回车键或更改焦点进行更改.
What I am trying to do is make the TextBlock respond to a (double)click which turns it into a TextBox. The user can then edit the description, and press return or change focus to make the change.
我尝试在与 TextBlock 相同的位置添加 TextBox 元素并使其可见 Collapsed,但我不知道如何导航到右侧 TextBox 当用户点击了 TextBlock 时.也就是说,我知道用户点击了某个TextBlock,现在我要显示哪个 TextBox?
I have tried adding a TextBox element in the same position as the TextBlock and making its visiblity Collapsed, but I don't know how to navigate to the right TextBox when the user has clicked on a TextBlock. That is, I know the user has clicked on a certain TextBlock, now which TextBox do I show?
任何帮助将不胜感激,
-Ko9
推荐答案
我在这些情况下所做的是使用 XAML 层次结构来确定要显示/隐藏的元素.类似的东西:
What I've done in these situations is used the XAML hierarchy to determine which element to show/hide. Something along the lines of:
<Grid>
<TextBlock MouseDown="txtblk_MouseDown" />
<TextBox LostFocus="txtbox_LostFocus" Visibility="Collapsed" />
</Grid>
用代码:
protected void txtblk_MouseDown(object sender, MouseButtonEventArgs e)
{
TextBox txt = (TextBox)((Grid)((TextBlock)sender).Parent).Children[1];
txt.Visibility = Visibility.Visible;
((TextBlock)sender).Visibility = Visibility.Collapsed;
}
protected void txtbox_LostFocus(object sender, RoutedEventArgs e)
{
TextBlock tb = (TextBlock)((Grid)((TextBox)sender).Parent).Children[0];
tb.Text = ((TextBox)sender).Text;
tb.Visibility = Visibility.Visible;
((TextBox)sender).Visibility = Visibility.Collapsed;
}
我总是把我要重用的类似的东西变成一个UserControl,我可以添加额外的错误处理,并保证Grid只会包含两个项目,它们的顺序永远不会改变.
I always turn stuff like this that I'm going to reuse into a UserControl, which I can add additional error handling to, and guarantee that the Grid will only contain two items, and the order of them will never change.
此外,将其转换为 UserControl 允许您为每个实例创建一个 Text 属性,因此您可以为每个实例命名并直接引用文本,而无需通过 <代码>((TextBox)myGrid.Children[1]).Text 铸造.这将使您的代码更加高效和干净.如果你把它做成一个用户控件,你也可以命名 TextBlock 和 TextBox 元素,所以根本不需要转换.
Additionally, turning this into a UserControl allows you to create a Text property for each instantiation, so you can name each one and reference the text directly without fishing for the current value through the ((TextBox)myGrid.Children[1]).Text casting. This will make your code much more efficient and clean. If you make it into a UserControl, you can also name the TextBlock and TextBox elements, so no casting is needed at all.
这篇关于使用数据模板 (WPF) 在 ListBox 中内联编辑 TextBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)