简体   繁体   English

从集合到自定义DataGridCell模板的嵌套对象的WPF动态DataGrid绑定值

[英]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. 目的是将数据(DataGrid)依次显示为具有列动态计数的几个网格,其中每个单元格(不仅是列)都可以通过双向绑定进行编辑,也可以不进行编辑。

I would like to avoid using code behind approach and I believe xaml can offer me what I need. 我想避免在方法后面使用代码,并且我相信xaml可以为我提供所需的东西。 Other thing is mvvm injection. 另一件事是mvvm注入。

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. 我的第一个难题是创建DataTable,但是此功能不能用于单元格可编辑级别。 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 然后我有另一个VM代表一个表(网格),其中包含VM

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

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

Then my xaml looks like this 然后我的xaml看起来像这样

<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 在XAML中绑定到WPF DataGridCell的内容时出现问题

(With code added as requested) (根据要求添加代码)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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