Ofir
Ofir

Reputation: 5279

Bind to ObservableCollection specific Index

I have the next class:

public class GridViewControlModel : NotificationObject
{
    DataTable datasource;
        public DataTable Datasource
        {
            get
            {
                return datasource;
            }
            set
            {
                datasource = value;
                RaisePropertyChanged("Datasource");
            }
        }

        ObservableCollection<string> fieldsNames;
        public ObservableCollection<string> FieldsNames
        {
            get
            {
                if (fieldsNames == null)
                {
                    fieldsNames = new ObservableCollection<string>();
                }
                return fieldsNames;
            }
            set
            {
                fieldsNames = value;
                RaisePropertyChanged("FieldsNames");
            }
        }    
//and more properties
}

The property "FieldsNames" contains list of columns Headers that I want bind to GridView data column header.

In my ViewModel I created property - GridViewControlModel CurrentGridViewData and I want to bind my GridView columns headers to my CurrentGridViewData.FieldsNames[indexes]

for example

<dxg:GridColumn1 Header="{Binding CurrentGridViewData, Path=CurrentGridViewData.FieldsNames[0]}">
<dxg:GridColumn2 Header="{Binding CurrentGridViewData, Path=CurrentGridViewData.FieldsNames[1]}">

and so on... Im working with DevExpress gridCointrol but Im don`t think its matter .

How can I bind property to specific ObservableCollection index?

I will explain what Im trying to do. I have GridControl with many views. When the user switch view the grid control need to change its entire structure (different number of columns, columns headers, column size, types and etc). I created a class which contains all the properties and settings for each view (this CurrentGridViewData class from my question) and when the user switch view I change the reference fo CurrentGridViewData to the right view instance and by binding I want to make all grid changes. Is there a better way?

Thanks

Thanks


I think I found possible solution related to DevExpress GridControl. Look at the next link http://documentation.devexpress.com/#wpf/CustomDocument10121

What are you thinking? Is it better approach than using DataTemplate?

Thanks

Upvotes: 0

Views: 1347

Answers (1)

Ucodia
Ucodia

Reputation: 7710

My solution for you would be to create a DataTemplate for each of the model you want to bind to your DataGrid.

For example lets say you have 3 different DataTables: OrdersDataTable, CustomersDataTable and ProductsDataTable.

First thing would be to replace your DataGrid control by a simple ContentControl.

<ContentControl Content="{Binding DataTable}"/>

Then add and create those DataTemplate in your application ressources.

<DataTemplate DataType="{x:Type local:OrdersDataTable}">
    <DataGrid ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding ID}" Header="ID"/>
            <DataGridTextColumn Binding="{Binding Tax}" Header="Tax"/>
            <DataGridTextColumn Binding="{Binding NetAmount}" Header="NetAmount"/>
        </DataGrid.Columns>
    </DataGrid>
</DataTemplate>

<DataTemplate DataType="{x:Type local:CustomersDataTable}">
    <DataGrid ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding ID}" Header="ID"/>
            <DataGridTextColumn Binding="{Binding FirstName}" Header="First name"/>
            <DataGridTextColumn Binding="{Binding LastName}" Header="Last name"/>
        </DataGrid.Columns>
    </DataGrid>
</DataTemplate>

<DataTemplate DataType="{x:Type local:ProductsDataTable}">
    <DataGrid ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding ID}" Header="ID"/>
            <DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
            <DataGridTextColumn Binding="{Binding Quantity}" Header="Quantity"/>
            <DataGridTextColumn Binding="{Binding UnitPrice}" Header="UnitPrice"/>
        </DataGrid.Columns>
    </DataGrid>
</DataTemplate>

This way, when you will bind your DataTable to the content control, it will lookup for the concrete DataTable type and crawl the application resources to find a corresponding DataTemplate to display your DataTable.

I recommend to use a Style for the DataGrid control so you don't have to reproduce the style in all the DataTemplate.

NB: The DataGrid.ItemsSource might not be correct for you, just adapt it.

Upvotes: 1

Related Questions