简体   繁体   中英

WPF dynamic DataGrid binding value from nested object from the collection to custom DataGridCell template

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM