如何在WPF和C#中的列表中移动对象 [英] How to move object in a list in wpf and c#

查看:56
本文介绍了如何在WPF和C#中的列表中移动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#和wpf开发应用程序.我的要求是从数据库检索数据并显示它.该应用程序的用户可以更改检索到的数据在列表中的位置.每个数据都是一个段落.如何达到这一要求.这 用户可以在列表中上下移动选定的段落.

I am developing an application using c# and wpf. My requirement is to retrieve data from database and display it. User of this application can change the position of retrieved data in the list. Each data is a paragraph. How to achieve this requirement. The user can move a selected paragraph up or down in the list.

到目前为止,我已经能够从数据库中检索数据.但是无法找到一种在列表中显示的方式,以便可以在列表中重新排列它们.希望你理解我的要求.

So far, I am able to retrieve the data from database. But could not able to find a way to display in a list so that those can be rearranged in the list. Hope you understand my requirement.

请注意,我想在wpf中执行此操作.

Please note, I want to do this in wpf.

尽可能多的细节将对您有很大帮助.

As much detail, that will be great help.

推荐答案

>>但无法找到一种显示在列表中的方法,以便可以重新排列它们在列表中.

>>But could not able to find a way to display in a list so that those can be rearranged in the list.

通常,您可以使用ItemsControl在WPF中显示项目列表.列表框是ItemsControl的示例,它使用户可以选择项目:

You typically use an ItemsControl to display a list of items in WPF. A ListBox is an example of an ItemsControl that enables the user to select items:

<ListBox x:Name="lb">
  <ListBox.ItemTemplate>
 <DataTemplate>
  <TextBlock Text="{Binding Name}" />
 </DateTemplate>
  </ListBox.ItemTemplate>
</ListBox>

ItemTemplate定义了ItemsControl的ItemsSource中每个项目的外观,即"paragraph"在你的情况下.请参考以下链接以获取更多信息: http://www.wpftutorial.net/ListBoxDataTemplate.html .

The ItemTemplate defines the appearance of each item in the ItemsControl's ItemsSource, i.e. the "paragraph" in your case. Please refer to the following link for more information: http://www.wpftutorial.net/ListBoxDataTemplate.html.

您将ItemsControl的ItemsSource属性设置或绑定到IEnumerable< T>.您要在列表中显示的项目中的一个:

You set or bind the ItemsSource property of the ItemsControl to an IEnumerable<T> of the items that you want to display in the list:

var yourCollectionOfItems = new ObservableCollection<YourType>();
//...
lb.ItemsSoure = yourCollectionOfItems;

您将必须自己实现重新安排部分.如果您使用ObservableCollection< T>作为源集合而不是例如List< T> ;,您可以使用其Move方法以编程方式将项目从一个索引移动到另一个索引. 请参阅我在以下主题中的答复,以获取有关此信息和示例的更多信息:
https://social.msdn.microsoft.com/Forums/sqlserver/zh-CN/2474a024-776c-41e9-a48e-a792d626ac7c/re-sorting-of-existing-list?forum=csharpgeneral

You will have to implement the rearrange part yourself. If you use an ObservableCollection<T> as your source collection instead of for example a List<T>, you could use its Move method to programmatically move an item from one index to another. Please refer to my reply in the following thread for more information about this and an example:
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/2474a024-776c-41e9-a48e-a792d626ac7c/re-sorting-of-existing-list?forum=csharpgeneral
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/12e1ea52-2754-4779-86d2-e1ee9801d3fa/problem-with-moving-items-up-and-down-in-checkedlistbox-using-observablecollection?forum=wpf

希望这可以帮助您入门.如果您在此过程中遇到一些更具体的问题,则始终可以启动一个新线程.不过,不要指望任何人为您实现整个示例应用程序:)

This should hopefully get you started. You could always start a new thread if you run into some more specific issue along he way. Don't expect anyone to implement an entire sample application for you though:)

希望有帮助,祝你好运.

Hope that helps and good luck.

请记住,通过将有用的帖子标记为答案来关闭话题,然后在遇到新问题时开始新话题.请不要在同一线程中问几个问题.

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


这篇关于如何在WPF和C#中的列表中移动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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