Hi
I have to add dynamic columns to my XamDataGrid, the thing is that for each dynamic columns I have a custom style template with a custom user control inside, this user control is almost the same as a combo box, it has items and it has a selected item.
My problem is that the grid have to bind to one source and my:ScoreControl have to bind to another source dinamycally generated, and the SelectedItem property of my:ScoreControl have to bind with the same source of my grid has.
This is easy in WinForm but I having problem in WPF, I wondering if anyone can help me.
<Style x:Key="id6" TargetType="{x:Type igDP:CellValuePresenter}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
ItemSelected="{Binding ???}"></my:ScoreControl>
</StackPanel>
</Setter.Value>
</Setter>
</Style>
Maybe I'm not on the right way, and please tell me if I'm not clear.
Hello Erwin,
ItemSelected - if you want to bind it to whatever the XamDataGrid is bound to, you have to use something like this:
Binding Path=DataPresenter.DataSource, RelativeSource={RelativeSouce TemplatedParent}.
This should bind to the DataPresenter property of the CellValuePresenter, which represents the XamDataGrid.
Items - I am not sure to what you want to bind this one. Can you please give us more specific details so that I can give you a proper xaml binding expression?
Hi Alex,
Of course and thanks for answer.
Items is a collection that has a value and display member (the same as combobox), for each column I have a collection to bind to my custom user control. I could put the collection in every row of my DataTable and my problem is solved (a DataTable is the datasource of my grid) but I think is not right, I have hundreds of rows in my DataTable, in this case the most efficient way to do it is to create resources dynamically in code behind, and then in the grid bind my usercontrol to the respective resource.
My problem is to do all that in code behind. Because all that would be generated dynamically.
Sorry for the late response.
I am not sure why one is an issue for you. You have to create three generic collections and add then as resources:
this.Resources.Add("key",collection1);
4) After you have created and bound your DataTable to the XamDataGrid you have to hook up the items sources of the controls. The best way would be to bind to the CellValuePresenter's Field property in order to determine which collection you have to extract from the resources and bind to it.
You can also hook up to the Loaded event and check the visual tree and find the parent CVP's Field.
The thing that is generated dynamically here you are a draw that explain my escenario better.
As you can see in the image, I have collections that are generated dynamically, the example you mentioned is using a xmldataprovider, In this case I think I have to follow the next steps:
1.- Create resources dynamically. (Collection X, Y and Z)
2.- Create my DataTable with Title, Velocity, Rated and Recommended columns
3.- Fill my DataTable with the title and for the other columns fill with the ID of the already selected item.
4.- Bind my custom user control of every column to the respective resource.
My problem is Step 1 and Step 4. I dont know how to do it dynamically :(
Ok, so the Items collection has to be bound somewhere outside the data source of the XamdataGrid. In this case ,you can use some data provider or static class to get the items from. You can see an example of a similar approac in the XamFeatureBrowser - XamDataGrid - Control Composition - XamComboEditor in DataRecord Cells. The sample demonstrates two XamComboEditor fields, which are bound to sources outside the XDG's DataSource.