从UserControl添加ListView列. [英] Add ListView column from an UserControl.

查看:145
本文介绍了从UserControl添加ListView列.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我想在设计时根据数据库添加listview列. Listview呈现给它另一个UserControl. 我该怎么办XAML.我已经尝试过依赖属性..但我无法实现它.请帮帮我. 问候 Jegan

Hi I want to add listview column according to the database at design time . Listview is present it another UserControl. How can I do it XAML. I have tried dependency property .. but i am not able to achive it . Please help me out. Regards Jegan

推荐答案

我假设您将ListView.View设置为GridView,并且在该GridView中,您将利用Columns属性来指定每个GridViewColumn.我不确定是否可以将GridView.Columns绑定到另一个集合,所以假设您不能.您将在UserControl上创建一个GridViewColumnCollection(该类型已经存在)属性,也许将其称为GridColumns.由于要从XAML填充此列表,因此必须使GridColumns成为依赖项属性.在Register方法(必须为所有依赖项属性调用)中,有一个接受PropertyMetadata的重载.在PropertyMetadata的构造函数之一中,它允许您指定一个委托来处理更改属性的事件.您将需要使用UserControl上的方法来注册该事件.我们将该方法称为HandleChangedColumns.此HandleChangedColumns方法将清空ListView DataGrid的Columns,然后使用GridColumns中的值指定的列(请记住,这是UserControl的依赖项属性)重新填充它们.这样,只要有人在XAML中使用您的用户控件并指定GridColumns的值,就会通知您的HandleChangedColumns方法,然后用您的UserControl用户指定的列填充ListView的列.
I''m assuming you are setting ListView.View to GridView and that, in that GridView, you would be making uses of the Columns property to specify each GridViewColumn. I''m not sure if you can bind GridView.Columns to another collection, so let''s assume you can''t. You would create a GridViewColumnCollection (that type already exists) property on UserControl, perhaps calling it GridColumns. Since you want to populate this list from XAML, you''d have to make GridColumns a dependency property. In the Register method (which you must called for all dependency properties), there is an overload which accepts PropertyMetadata. In one of the constructors for PropertyMetadata, it allows you to specific a delegate to handle the event of the property being changed. You will want to sign up to that event with a method on your UserControl. Let''s call that method HandleChangedColumns. This HandleChangedColumns method will empty the ListView DataGrid''s Columns, then repopulate them using the columns that were specified by the value in GridColumns (remember, that was the dependency property on your UserControl). That way, whenever somebody uses your user control in XAML and specifies the value of GridColumns, your HandleChangedColumns method will be notified and it will then fill the ListView''s columns with the ones specified by the user of your UserControl.


让我看看我是否正确理解你.您希望能够在主窗口的XAML中指定应在主窗口使用的UserControl中的ListView中使用哪些列?而且您希望在设计时全部完成此操作,因此您不需要运行时逻辑(例如,进入数据库并获取每一列的名称和类型).您到底要在这些列中指定什么(列名,数据类型,显示该列的模板等)?为什么不只在实际的ListView中定义列,而不是在使用包含ListView的UserControl的XAML中定义列?
Let me see if I understand you correctly. You want to be able to specify, in your main window''s XAML, which columns should be used in a ListView that is in the UserControl used by the main window? And you want to do this all at design time, so you don''t have any need for runtime logic (e.g., going out to a database and getting the name and type of each column). What exactly do you want to specify in these columns (column name, data type, a template to display the column, etc.)? And why don''t you just define the columns in the actual ListView rather than in the XAML that consumes the UserControl that contains the ListView?


我不知道如何回复你回答.所以我将其添加为答案... 您说对了..我将解释整个情况. 我想要导航4个按钮,如First,Previous,Next,Last和UserControl中的listview控件. 这样我就可以在设计时在不同地方使用此用户控件... 因此,我们需要一个窗口来填充一张表中的记录.在第二个窗口中,我们必须填充第二个表中的记录. 也就是说,我想在用户控件中使用此listview,并希望在设计时根据窗口设计列. 很抱歉张贴这个答案 问候 杰根
I don''t know how to reply to you answer . so i am adding this as an answer... You got it correct.. I will explain the full scenario. I want navigation 4 buttons like First,Previous,Next,Last with listview control in an UserControl. So that i can use this usercontrol in different places at design time... So One window we need to fill the records from one table. and in second window we have to fill records from second table. That is y i want use this listview in usercontrol and want to design the columns at design time according to the windows. sorry for posting this an answer Regards Jegan


这篇关于从UserControl添加ListView列.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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