I mean i need to bind unbound fields programmatically but i cannot manage to bind to a DataSource of type ObservableCollection<ObservableCollection<String>> sCol :
<igDP:XamDataGrid x:Name="xamDataGrid" DataSource="{Binding sCol}"> <igDP:XamDataGrid.FieldLayoutSettings> <igDP:FieldLayoutSettings AutoGenerateFields="False" /> </igDP:XamDataGrid.FieldLayoutSettings> <igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout> <igDP:FieldLayout.Fields> <igDP:UnboundField Label="Col1" BindingPath="[0]" BindingMode="TwoWay" DataType="sys:String" /> <igDP:UnboundField Label="Col2" BindingPath="[1]" BindingMode="TwoWay" DataType="sys:String"/> </igDP:FieldLayout.Fields> </igDP:FieldLayout> </igDP:XamDataGrid.FieldLayouts> </igDP:XamDataGrid>
Ouput traces say :
System.Windows.Data Error: 39 : BindingExpression path error: '[' property not found on 'object' ''EnumerableObjectWrapper' (HashCode=14407208)'. BindingExpression:Path=[1]; DataItem='EnumerableObjectWrapper' (HashCode=14407208); target element is 'a' (HashCode=44344604); target property is 'Value' (type 'Object')
I can achive a correct binding through a regular listView though :
<ListView ItemsSource="{Binding sCol}"> <ListView.View> <GridView> <GridViewColumn Header="Col1" DisplayMemberBinding="{Binding [0]}"/> <GridViewColumn Header="Col2" DisplayMemberBinding="{Binding [1]}"/> </GridView> </ListView.View> </ListView>
It seems that the grid DataSource property does not behave like the list ItemsSource property.
What am I missing ?
Cheers.
Just to let you know that I also managed to make the Xceed grid works in the same context :
<xcdg:DataGridControl Name="xtraGrid" ItemsSource="{Binding Source={StaticResource xtraGridView}}" AutoCreateColumns="False" > <xcdg:DataGridControl.Columns> <xcdg:Column FieldName="[0]" Title="Col1" /> <xcdg:Column FieldName="[1]" Title="Col2" /> </xcdg:DataGridControl.Columns> </xcdg:DataGridControl>
We are actually trying various WPF datagrids on the market to make our choice for my job. I would be very disappointed if we could not go further with your grid. I m sure it's just my mistake and not an issue with your design.
Please help :-)
Hi,
Has anyone found a solution to this problem?
We are experiencing exactly the same thing. In C# only an indexer can take a parameter so you have to be able to specify a parameter without a property as this post describes, i.e. FieldName="[0]".
Cheers,
Dave
Hi Alex,
Thanks for the suggestion. We have worked around this problem in a similar way by implementing model wrappers around our data classes in the UI (for more reasons than just solving this problem). It's good to know there's a c# only solution though.
Hello,
I am following up on this post to provide additional details. Internally an EnumerableObjectWrapper gets used to wrap lists of lists when the XamDataGrid is bound to them and this is why the BindingPath of "[0]" will fail. The EnumerableObjectWrapper does expose an Items collection that you can use in the BindingPath: "Items[0]"
Here is a code snippet that demonstrates this:
XamDataGrid xamDataGrid1 = new XamDataGrid(); this.Content = xamDataGrid1; xamDataGrid1.FieldLayoutSettings.AutoGenerateFields = false; ObservableCollection<string> headers = new ObservableCollection<string> { "FirstName", "LastName", "Age" }; ObservableCollection<string> row1 = new ObservableCollection<string> { "John", "Doe", "19" }; ObservableCollection<string> row2 = new ObservableCollection<string> { "Jane", "Doe", "21" }; ObservableCollection<string> row3 = new ObservableCollection<string> { "Suzie", "Q", "52" }; ObservableCollection<string> row4 = new ObservableCollection<string> { "No", "Body", "48" }; ObservableCollection<ObservableCollection<string>> tableValues = new ObservableCollection<ObservableCollection<string>> { row1, row2, row3, row4 }; FieldLayout fldLayout = new FieldLayout(); for (int i = 0; i < headers.Count; i++) { string s = headers[i]; UnboundField uf = new UnboundField(); uf.Name = s; uf.Label = s; uf.BindingPath = new PropertyPath("Items[" + i.ToString() + "]",null); fldLayout.Fields.Add(uf); } xamDataGrid1.FieldLayouts.Add(fldLayout); xamDataGrid1.DataSource = tableValues;
Hi Alan,
Is there any MVVM solution for the above where we can change the FieldLayout in the ViewModel?
Regards,
Prasanna
Prasanna,
It isn't possible to have a binding on the FieldLayout because it isn't actually part of the visual or logical trees. Assuming that the number of fields will still change dynamically you would be best off creating a UserControl that contains the XamDataGrid that exposes a property that you can set for the DataSource and Headers and then when those properties change update your FieldLayout accordingly. While this would require writing code in the UserControl, when you consume the UserControl you could still use MVVM.
Let me know if you have any questions with this matter.
Can you give me a sample with this approach.
If not can you look at my requirement below and suggest some MVVM approach. There are two types of fields a user can select namely "Periods" and "Abbrev". Upon selecting the two sets of fields, it results in a Flat Grid (see Figure 1 below). I need to convert this flat grid into another grid as shown in Figure 2 below.
Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4
Name
Period
Abbrev
Value
abc
CY
Unadj
10
Adj
17
Final
21
PY
1000
2000
Fig 1
77
xyz
45
50
23
3000
4000
88
Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4 Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4
Unadj CY
Adj CY
Final CY
Final PY
Fig 2
The example that I provided earlier in this thread is the only existing example that I have. You would have to modify this so that the header and row collections could be exposed as properties on a user control and then when the properties are set update the XamDataGrid appropriately.
Even if this is only going to be used in one place in your application it would still make sense to use the XamPivotGrid since the control already provides the functionality that you are looking for.
Hi Alan,Thanks for your reply. But this is a one-off requirement so I am looking for a XamDataGrid solution. Can you please elaborate on how to expose a property for the Headers and then update FieldLayout accordingly. If you have a sample or already posted solution please let me know.Regards,Prasanna
I would recommend using the XamPivotGrid rather than the XamDataGrid which could display the data in this way when binding to a flat data source. You can find more details on the XamPivotGrid here:
http://ko.infragistics.com/dotnet/netadvantage/wpf/data-visualization/xam-pivot-grid.aspx#Overview