在数据模板的列表框内嵌编辑的TextBlock(WPF) [英] Inline editing TextBlock in a ListBox with Data Template (WPF)

查看:131
本文介绍了在数据模板的列表框内嵌编辑的TextBlock(WPF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用WPF,我有一个列表框里面用的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 回应一个(双)点击该把它变成一个文本框。然后,用户可以编辑说明,然后按回车键或更改焦点进行更改。

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元素,并使得它的可视性折叠 ,但我不知道如何导航到合适的文本框当用户点击了一个的TextBlock 。也就是说,我知道用户点击了某个的TextBlock ,现在的 文本框我说明了什么?

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>



与代码:

with the code:

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;
}



我总是把这样的东西,我要重用成用户控件,我可以添加额外的错误处理,并保证在电网将只包含两个项目,以及订单。的他们永远不会改变。

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.

编辑:此外,把这个变成一个用户控件允许你创建一个文本属性对于每个实例,所以你可以命名每一个并引用文本的情况下直接捞通过((文本框)myGrid.Children [1])。文字铸造的当前值。这将使你的代码更高效,清洁。如果你把它做成用户控件,你也可以命名的TextBlock 文本框元素,因此不需要铸造在所有

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.

这篇关于在数据模板的列表框内嵌编辑的TextBlock(WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆