Hi, I have recently started using Xamdatagrid in my project. My datasource is something say "MyObject" Which has Datas list and i want to generate columns in this dynamically as at run time.Lets say viewmodel has datasource as
Class ViewModel
{
public list<MyObject> MyObjects{get;set;}
}
Class MyObject
Public string Name{get;set;}
public int Id{get;set;}
public List<Data> Datas{get;set;}
MyObject()
Datas = new List<Data>();
Class Data has value property in it as
Class Data
public string DataName {get;set;}
public int Value{get;set;}
Xaml is as follows
<Style x:Key="MyStyle" TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Grid>
<TextBox Text="{Binding Path=Value}" />
</Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
<Grid> <igWPF:XamDataGrid DataSource="{Binding Path=MyObject}" FieldLayoutInitialized="xamDataGrid_FieldLayoutInitialized"> <igWPF:XamDataGrid.FieldLayoutSettings> <igWPF:FieldLayoutSettings AutoGenerateFields="False"/> </igWPF:XamDataGrid.FieldLayoutSettings> <igWPF:XamDataGrid.FieldLayouts> <igWPF:FieldLayout> <igWPF:Field Name="Name"/> <igWPF:Field Name="Id"/> </igWPF:FieldLayout> </igWPF:XamDataGrid.FieldLayouts> </igWPF:XamDataGrid> </Grid>
I am creating Dynamic columns of Data and applying style MyStyle in my code behind where i have hooked to an event xamDataGrid_FieldLayoutInitialized as follows:
private void xamDataGrid_FieldLayoutInitialized(object sender, Infragistics.Windows.DataPresenter.Events.FieldLayoutInitializedEventArgs e){ var MyObjects = this.ViewModel.MyObjects.First(); for (Int32 i = 0; i < MyObjects.Datas.Count; i++) { var field = new UnboundField { Name = MyObjects.Datas[i].DataName, };
field.Settings.CellValuePresenterStyle = this.FindResource("MyStyle") as Style; e.FieldLayout.Fields.Add(field); }}
But when i run App it fails to Bind Property Value in Data as i see it in OutPut window."Can not find property'Value'"
Suppose my list of Datas has only one Item in it and if i define my style as
<Style x:Key="MyStyle" TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Grid DataContext = "{Binding Path=DataItem.Datas[0]}">
Then it displays one dynamical created cloumn.
Or if i define style as
<Style x:Key="MyStyle" TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Grid >
<TextBox Text="{Binding Path=DataItem.Datas[0].Value}" />
Then also it displays one dynamically created column.
As i want to generate my columns dynamically , which will contain more than one columns, i want following style to be generic and that can be applied to all index.Without explicitly specifying index.
I know it has to do with Binding Value property correctly in my style. How can i accomplish this.
I Know i can do something like this where i am creating dynamic columns
private void xamDataGrid_FieldLayoutInitialized(object sender, Infragistics.Windows.DataPresenter.Events.FieldLayoutInitializedEventArgs e){ var MyObjects = this.ViewModel.MyObjects.First(); for (Int32 i = 0; i < MyObjects.Datas.Count; i++) { var field = new UnboundField { Name = MyObjects.Datas[i].DataName,
BindingPath = new PropertyPath(String.Format("Datas[i].Value",i)) };
But I dont want this approach as in my real company project Data object is complex which has other properties other than Value which i am using in MyStyle. Hemce i dont want to bind to only one property "Value" in my Style.
Hello Rony,
Thank you for your post.
I have been looking into it and it seems like I am missing something from your scenario. If this is still an issue for you, could you please send me an image that illustrates what you want to achieve and how the XamDataGrid should look?
Looking forward to hearing from you.