简体   繁体   中英

How to organize ItemSource for such DataGrid

I'm Using MVVM and can't figure out how to organize ItemSource class for such DataGrid.

DataGrid XAMl code:

<DataGrid  CanUserAddRows="True"
                   ItemsSource="{Binding TestCollection}">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="First Column">
                    <DataGridTemplateColumn.CellEditingTemplate >
                        <DataTemplate >
                            <ContentControl>
                                <StackPanel>
                                    <TextBox Text="{Binding First}"/>
                                    <TextBox Text="{Binding Second}"/>
                                </StackPanel>
                            </ContentControl>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Second Column">
                    <DataGridTemplateColumn.CellEditingTemplate >
                        <DataTemplate >
                            <ContentControl>
                                <StackPanel>
                                    <TextBox Text="{Binding Third}"/>
                                    <TextBox Text="{Binding Fourth}"/>
                                </StackPanel>
                            </ContentControl>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
</DataGrid>

I have this idea, but it doesn't work.

 public class ComplexTable
{
    public ComplexTable()
    {
        FirstProperty = new FirstClass();
        SecondProperty = new Second();    
    }
    public class FirstClass
    {
        public FirstClass()
        {
            First = "FirstString";
            Second = "SecondString";
        }
        public string First { get; set; }
        public string Second { get; set; }
    }

    public class Second
    {
        public Second()
        {
            Third = "ThirdString";
            Fourth = "FourthString";
        }
        public string Third { get; set; }
        public string Fourth { get; set; }
    }

    public FirstClass FirstProperty { get; set; }
    public Second SecondProperty { get; set; }
}

public ObservableCollection<ComplexTable> _testCollection = new ObservableCollection<ComplexTable>();
private ObservableCollection<ComplexTable> TestCollection
{
    get { return _testCollection; }
    set
    {
        _testCollection = value;
        RaisePropertyChanged("TestCollection");
    }

}

As i understand each property in class for ItemSource responsible for column in DataGrid, and if cell contains several controls etc. property Type should consist property for each control as well. Cant figure out where am i wrong.

http://www.codeproject.com/Articles/683429/Guide-to-WPF-DataGrid-formatting-using-bindings

You can understand with this link.

 <StackPanel>
        <TextBox Text="{Binding SelectedItem.Name, ElementName=myDataGrid}"/>
        <DataGrid x:Name="myDataGrid" />
    </StackPanel>

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