Hello
I need to create a custom control using XamGrid. Could you suggest a code sample how do it?
I need custom control with a textbox and XamGrid.
http://prntscr.com/g6ba9z
And I when I will use this control I want to define columns in a place where it using.
And I stuck with a problem that I can't find a way how to use TemplateBinding for XamGrid.Columns. Do you have any idea?
Hello Andrey,
In order to achieve your requirement in this case, I would recommend using a UserControl. In doing so, you could set up the visual tree of that particular control to have a TextBox and a XamGrid inside, so that when you use it, these elements would show up together.
If you give the XamGrid in your UserControl an x:Name, you can access it using that name. For example, you could do something like the following when using the UserControl programmatically:
MyUserControl control = new MyUserControl();XamGrid grid = control.theGrid as XamGrid;
From there, you could manually add the columns to the XamGrid.Columns collection.
I am rather unsure why you are trying to use a TemplateBinding for the Columns collection of the XamGrid, but this will not work at the moment, as the XamGrid.Columns collection is not a DependencyProperty, and as such, cannot be bound. Perhaps you could create a derivation of the XamGrid in which you add a DependencyProperty that talks to the Columns collection of the underlying XamGrid when it is changed?
I have attached a sample project to demonstrate the UserControl approach mentioned above. I hope this helps.
Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate Developer
Thanks for the project but it is not appropriate for us. We not able to manage a grid object in a code behind.
"Perhaps you could create a derivation of the XamGrid in which you add a DependencyProperty that talks to the Columns collection of the underlying XamGrid when it is changed?"
Could you help me with this? I trying to do something like this, but have not got any result.
I attached a project where I made some implementation.
The sample project that you have attached is along the lines of what I had meant by that line. Essentially, the idea in this case is that you would create a custom XamGrid which has a new DependencyProperty taking a collection<Column>, and you would hook a property changed callback for that property. In this callback, you can hook the CollectionChanged event on it for dynamic Column adding and removing, as well as adding or removing the Column elements to or from the actual XamGrid.Columns collection. In doing so, you will be able to bind a collection of columns to the XamGrid and have them be reflected in the grid.
Another thing that you may want to consider doing is to hook into the XamGrid.Columns.CollectionChanged event in your custom XamGrid in the case that you decide to add the columns directly to that collection instead. If you were to do this, you would need to synchronize the custom "ColumnsCollection" property with what is being added or removed from the custom XamGrid directly.
I have attached a sample project to demonstrate how this could be achieved. I hope this helps.
It is also worth noting that with the original UserControl sample that I had written previously, that this did not necessarily have to be handled in code-behind. You could just as easily write a custom Behavior<UserControl> or a Behavior<XamGrid> for the elements in that UserControl to handle these events in a more MVVM-friendly way. In case you are unfamiliar with creating behaviors in WPF, I would recommend taking a look at the following article: https://msdn.microsoft.com/en-us/library/ff725476(v=expression.40).aspx.