I am developing a custom control. My Custom Control has a listview in it which is suppose to show multiple fields from the binding EF Entity. The output would be something like this

Now what would be the data source for listview as all my Entities has different properties so i am not sure about the binding properties.
Right now my ListView has one image control, two textblock and one
link labels, how should i determine with which control which property
should bind. >I like to use this control in a RecordListing screen, say ‘Client
screen bind to Client entity’ and ‘Employee screen bind to Employee
entity’.There should be one entity at a time in my control,
Kindly guide me how could i do this is a very genric and logical way.
Thanks
Like Foovanadil suggested, I would expose a DataSource DependencyProperty. But over and above that I would also expose 5 more string dependency properties. The consumer of the control would then put the names of the properties they want to go in the specific controls.
Let me be more specific:
Think about how a Combobox works, where you can bind their DataSource, but you can also supply a
DisplayMemberPathand aSelectedValuePaththat specify which properties in the datasource to use.You could do the same thing with your control:
Expose an “ImagePathMember” property. This will be the Name of the property that contains the path for the image that goes in the image control
Expose a “LinkPathMember” property. This property will be the Name of the property that contains the link’s path
Expose a “LinkDisplayMember” property. This property will be the Name of the property that contains the text that the link will look like.
Expose a “TopTextBlockMember” property. This property will be the Name of the property that contains the text for the top textblock
Expose a “BottomTextBlockMember” property. This property will be the Name of the property that contains the text for the bottom textblock
Then you just use reflection in the control to determine the value for each listbox item and then bind it to the listboxitem’s image control, link, and 2 textblocks
Hope that helps
u_u
EDIT
Ok you asked for some code to point you in the right direction.
First off: Dependency Properties
Code Behind
As it turns out we don’t even need reflection. When you use bindings in code, you actually supply a string name for the property, which is handy because the dependency properties we created are really string names of the properties. Lucky us!
I wrote this code by hand so its probably all buggy. Also I’m a little worried about the binding, I didn’t set a
Sourcebecause I thought it would bind to the item in the collection, but I’m not sure of this behaviour.So I’m going to post this now and then give it a test run, and adapt it where required.
Good Luck
u_u
EDIT 2
Ok so things turned out to be alot more complex then I imagined. When the TextBlocks are in a DataTemplate you can’t really access them by just calling their name in the code behind.
What you have to do is wait for the ListBox/ListView to generate its items’ containers, then use the visualtreehelper to loop through all the children of the listview to find the specific controls you are looking for and then bind to them.
This took me such a long time to do because I couldn’t find the controls, because I attached an event handler to the ListView’s ItemsSourceChanged event, which meant I looked as the ItemsSource property changed, but before the containers for these items was generated.
Eventually I found a solution:
XAML:
In the template of your ListView/ListBox where you have the controls, you need to name them like so:
You also need to give a name to your listbox/listview, like so (and bind its ItemsSource to your DataSource property):
You will see that the binding has
ElementName=me. This is because I am binding to the actual control I am in (i.e. the MyCustomControl). MyUserControlhasx:Name="me"above thexmlns, as this makes it easy for me to bind to properties in the code behind.RecreateBindings:
You basically need to revamp the RecreateBindings method. I made a big mistake with my first post, as it needs to be a static method in order to run in the DependencyProperty’s PropertyChangedCallBack (i really shouldn’t have done the code by hand).
This is what I ended up with:
As you can see, now you need a RecreateBindings method for all the different types of controls in your listview/listbox. There is prolly a more generic way of doing it, but you can sort that out yourself. I can’t be doing all the work 😛
What the code is doing is it goes through the items in the listbox and gets their container. The ImageControl, once generated, will be a child of that container. So we go get the ImageControl via the help of the FindDescendants method I adapted from this post.
Here is that method:
FindDescendant method
The only adaptation I did was add the check for the name of the control. We have 2 TextBlocks in our ListBoxItem, so the original method would only return the first one. We need to check the names so we can do bindings on both.
PropertyCallBack Methods:
So because the RecreateBindings method was split up, we need to change the PropertyChangedCallBacks to call the RecreateBindings methods specific to each property. The datasource property will have all the RecreateBindings methods in it.
Please note here that MyCustomControl is the Type of this control you are creating.
Constructor and attached event handler:
Lastly we need to add a line to the constructor that adds an event handler to the ListBox’s ItemContainerGenerator, so we can check when the item container is generated, and we can attach our bindings.:
That should be it. If you need any help/have any questions let me know
u_u