Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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.
 
Share this answer
 
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?
 
Share this answer
 
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
 
Share this answer
 
Thanks for the answers.....

It is working....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900