Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
65
How to bind ti list of objects using xamDatagrid
posted

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]}">

                              <TextBox Text="{Binding Path=Value}" />

                   </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

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}" />

                   </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

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.

<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>

I know it has to do with Binding Value property correctly in my style. How can i accomplish this.

Thanks


Parents
No Data
Reply
  • 65
    posted

    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))
            };

           field.Settings.CellValuePresenterStyle = this.FindResource("MyStyle") as Style;
     
            e.FieldLayout.Fields.Add(field);
        }
    }

    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.

Children