1

I've been spending more then half a day to get this task sorted and for a tree I cannot see the forest.

The aim is to display data (DataGrid) as several grids in sequence with dynamic count of columns, where each cell (not just column) can or cannot be editable with two way binding.

I would like to avoid using code behind approach and I believe xaml can offer me what I need. Other thing is mvvm injection.

Lets make it simple and do binding for one table first.

My first tough was to create DataTable but this on cannot be used for cell editable level. Then I created collection of collections of objects (for one table -> many rows -> many columns).

public class DataGridCell : BaseViewModel
{     
    public string Value
     ....
    public bool IsEditable
     ....     

}

Then I have another VM which represents one table (grid) which contains VM upon

public class DataGridItem : BaseViewModel
{    
    public string TableName
    {
     ....
    }

    public ObservableCollection<ObservableCollection<DataGridCell>> Data
    {
     ....
    }
}

Then my xaml looks like this

<DataGrid ItemsSource="{Binding Path=Data}" AutoGenerateColumns="True">
    <DataGrid.Resources>

        <DataTemplate x:Key="ReadonlyCellTemplate">
            <TextBlock Text="{Binding Value}" />
        </DataTemplate>

        <DataTemplate x:Key="EditableCellTemplate">
            <TextBox Text="{Binding Value}" />
        </DataTemplate>

    </DataGrid.Resources>
    <DataGridTemplateColumn CellTemplate="{StaticResource ReadonlyCellTemplate}">
        <DataGridTemplateColumn.CellEditingTemplate>
            <DataTemplate>

                <ContentPresenter x:Name="Presenter" ContentTemplate="{StaticResource ReadonlyCellTemplate}" />
                <DataTemplate.Triggers>                             

                    <DataTrigger Binding="{Binding IsEditable, PresentationTraceSources.TraceLevel=High}" Value="True">
                        <Setter TargetName="Presenter" Property="ContentTemplate" Value="{StaticResource EditableCellTemplate}" />
                    </DataTrigger>

                </DataTemplate.Triggers>
            </DataTemplate>
        </DataGridTemplateColumn.CellEditingTemplate>
    </DataGridTemplateColumn>
</DataGrid>

The idea is to dynamically choose which cell will be filled in by data input controls and which not. Problem is binding. I cannot figure out how to bind concrete cell element in the collection.

Thanks for any possible advice

daniele3004
  • 13,072
  • 12
  • 67
  • 75
st35ly
  • 1,215
  • 18
  • 24

1 Answers1

0

I hope it's not too late but this is how I solved my issues with binding cells to dynamic data:

Problems binding to a the content of a WPF DataGridCell in XAML

(With code added as requested)

Community
  • 1
  • 1
Kezza
  • 736
  • 9
  • 25
  • hm, not really :) good solution is to use convertors on each cell data property in datagrid and findancestor binding approach method to find the cell object (DataGridCell) ... anyway its little bit late and I sorted out by combination above + code behind handling of editable fields :( but thanks for a giving me an idea to overload DataTemplate, looks interesting to me – st35ly Nov 10 '14 at 03:28