是否有一种优雅的方法可以在WPF中编辑列表框条目? [英] Is there an elegant way to make listbox entries editable in WPF?

查看:105
本文介绍了是否有一种优雅的方法可以在WPF中编辑列表框条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信这是一个非常常见的要求,但谷歌搜索到目前为止未能让我更明智地制作 ListBox 可编辑就地。



请注意,我仍然需要列表中项目当前所选项目的概念。



我的 ListBox 定义如下:



I have what I am sure is an extremely common requirement, but Googling around has so far failed to leave me any wiser about the neatest way to make the items of a ListBox editable in-place.

Note that I do still require the notion of a currently-selected item for the items in the list.

I have a ListBox defined as follows:

<DataTemplate x:Key="DataTemplateContact">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="0" Text="Name:"/>
        <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding FullName}"/>
        <TextBlock Grid.Row="1" Grid.Column="0" Text="Telephone:"/>
        <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding MainTelephoneNumber}"/>
    </Grid>
</DataTemplate>

<ListBox ItemsSource="{Binding Contacts}" ItemTemplate="{StaticResource DataTemplateContact}"/>





如果我点击某个项目的死部分,WPF选择更改机制将启动并设置当前所选项目,这很棒。问题是如果我点击任何一个 TextBox 控件。我可以开始输入这些方框,但据我所知,WPF似乎并没有意识到我正在做任何事情。



如果我设置< TextBox
字段上的code> IsEnabled =False然后修复了部分问题。如果我单击 TextBox ,WPF会识别出当前所选项目已更改,并在View-Model中相应地设置。然后问题就变成了如何判断 ListBox 中当前正在编辑的项目何时失去焦点。



谁能指点我一个简洁的解决方案?我可以使用触发器做点什么吗?



任何帮助都会非常感激。



亲切的愿望~Patrick



我的尝试:



我有通过处理UI中的大量事件将一半拼凑在一起(例如 ListBox.SelectionChanged ListBox.MouseDoubleclick ,以及试图抓住窗口中其他任何地方的点击次数),但这一切都让人觉得非常邋and,并在我的XAML代码隐藏文件中添加了一些令人不安的逻辑。



我有最近试过这个:





If I click on a 'dead' part of an item, the WPF selection-changed mechanism kicks in and sets the currently-selected item, which is great. The problem is if I click on either of the TextBox controls. I can begin typing in these boxes, but as far as I can tell, WPF doesn't seem to be aware that I am doing anything.

If I set IsEnabled="False" on the TextBox fields then this fixes part of the problem. If I click on a TextBox, WPF recognises that the currently-selected item has changed and this is set accordingly in my View-Model. The problem then becomes how to tell when the currently-being-edited item in the ListBox loses focus.

Can anyone please point me at a neat solution? Can I maybe do something using triggers?

Any help at all would be very much appreciated.

Kind wishes ~ Patrick

What I have tried:

I have half-cobbled something together by handling loads of events in the UI (e.g. ListBox.SelectionChanged, ListBox.MouseDoubleclick, and trying to catch clicks anywhere else in the window), but this all feels very scruffy and puts an uncomfortable amount of logic in my XAML code-behind file.

I have recently tried this:

<Style x:Key="TestStyle" TargetType="TextBox">
    <Setter Property="Focusable" Value="False"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
            <Setter Property="Background" Value="Crimson"/>
            <Setter Property="Focusable" Value="True"/>
        </DataTrigger>
    </Style.Triggers>
</Style>





深红色设置正常,但TextBox不可编辑。任何人都可以解释一下原因吗?



The crimson colour is set okay but the TextBox doesn't become editable. Could anyone please explain why?

推荐答案

嗨Patrick,



来自我们的对话我建议使用ItemsControl的。此WPF控件具有ItemsSource属性,您可以将其绑定到客户/联系人列表,并且很容易设置样式并设置ItemTemplate。它需要学习如何操作,但这并不困难,你将摆脱ListBox的问题。



ItemControl Class(System.Windows.Controls) [ ^ ]

http://www.wpf-tutorial.com/list-controls/itemscontrol / [ ^ ]
Hi Patrick,

from our conversation I would suggest to use ItemsControl. This WPF control has ItemsSource property that you can bind to your list of customers/contacts and is easy to style and set ItemTemplate. It requires to learn how to do it but it's not difficult and you will get rid of the problems you have with ListBox.

ItemsControl Class (System.Windows.Controls)[^]
http://www.wpf-tutorial.com/list-controls/itemscontrol/[^]


嗨帕特里克 - 听起来你正在描述IEditableCollectionView支持的场景。这允许您在显示模板和可编辑模板之间前后交换。我将介绍如何处理此此处 [ ^ ]。
Hi Patrick - it sounds like you're describing the scenarios supported by IEditableCollectionView. This allows you to swap backwards and forwards between a display template and an editable template. I describe how to deal with this here[^].


这篇关于是否有一种优雅的方法可以在WPF中编辑列表框条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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