<small id='543vg'></small><noframes id='543vg'>

    1. <i id='543vg'><tr id='543vg'><dt id='543vg'><q id='543vg'><span id='543vg'><b id='543vg'><form id='543vg'><ins id='543vg'></ins><ul id='543vg'></ul><sub id='543vg'></sub></form><legend id='543vg'></legend><bdo id='543vg'><pre id='543vg'><center id='543vg'></center></pre></bdo></b><th id='543vg'></th></span></q></dt></tr></i><div id='543vg'><tfoot id='543vg'></tfoot><dl id='543vg'><fieldset id='543vg'></fieldset></dl></div>

      <legend id='543vg'><style id='543vg'><dir id='543vg'><q id='543vg'></q></dir></style></legend>

        <tfoot id='543vg'></tfoot>
        • <bdo id='543vg'></bdo><ul id='543vg'></ul>
      1. 使用数据模板 (WPF) 在 ListBox 中内联编辑 TextBlock

        Inline editing TextBlock in a ListBox with Data Template (WPF)(使用数据模板 (WPF) 在 ListBox 中内联编辑 TextBlock)
          <tbody id='nOIxe'></tbody>

            <i id='nOIxe'><tr id='nOIxe'><dt id='nOIxe'><q id='nOIxe'><span id='nOIxe'><b id='nOIxe'><form id='nOIxe'><ins id='nOIxe'></ins><ul id='nOIxe'></ul><sub id='nOIxe'></sub></form><legend id='nOIxe'></legend><bdo id='nOIxe'><pre id='nOIxe'><center id='nOIxe'></center></pre></bdo></b><th id='nOIxe'></th></span></q></dt></tr></i><div id='nOIxe'><tfoot id='nOIxe'></tfoot><dl id='nOIxe'><fieldset id='nOIxe'></fieldset></dl></div>

          • <legend id='nOIxe'><style id='nOIxe'><dir id='nOIxe'><q id='nOIxe'></q></dir></style></legend><tfoot id='nOIxe'></tfoot>

                • <bdo id='nOIxe'></bdo><ul id='nOIxe'></ul>

                  <small id='nOIxe'></small><noframes id='nOIxe'>

                • 本文介绍了使用数据模板 (WPF) 在 ListBox 中内联编辑 TextBlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 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 铸造.这将使您的代码更加高效和干净.如果你把它做成一个用户控件,你也可以命名 TextBlockTextBox 元素,所以根本不需要转换.

                  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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                  • <tfoot id='7U243'></tfoot>

                      • <small id='7U243'></small><noframes id='7U243'>

                          <tbody id='7U243'></tbody>
                          <bdo id='7U243'></bdo><ul id='7U243'></ul>

                            <legend id='7U243'><style id='7U243'><dir id='7U243'><q id='7U243'></q></dir></style></legend>
                            <i id='7U243'><tr id='7U243'><dt id='7U243'><q id='7U243'><span id='7U243'><b id='7U243'><form id='7U243'><ins id='7U243'></ins><ul id='7U243'></ul><sub id='7U243'></sub></form><legend id='7U243'></legend><bdo id='7U243'><pre id='7U243'><center id='7U243'></center></pre></bdo></b><th id='7U243'></th></span></q></dt></tr></i><div id='7U243'><tfoot id='7U243'></tfoot><dl id='7U243'><fieldset id='7U243'></fieldset></dl></div>