How to bind collection to datagrid column in WPF

时间秒杀一切 提交于 2020-01-06 06:48:38

问题


I have the following model:

public class Order
{
 public Order()
 {
    Specifications = new List<string>();
 }
 public Guid Id { get; set; }
 public DateTime CreateDate { get; set; }
 public ICollection<string> Specifications { get; set; }
}

 public class Context: INotifyPropertyChanged
 {
    private ObservableCollection<Order> orders;
    public ObservableCollection<Order> Orders
    {
        get { return orders; }
        set
        {
            orders = new ObservableCollection<Order>(value);

        }
    }
 }

I am binding the collection to datagrid in Xaml using:

<Window.DataContext>
        <viewModel:Context/>
</Window.DataContext>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Orders}" HorizontalAlignment="Stretch" Margin="5" Name="dgUserList" VerticalAlignment="Stretch" >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Id}"/>
                <DataGridTextColumn Binding="{Binding CreateDate}" />
            </DataGrid.Columns>
</DataGrid>

How can I add another column to bind Specifications? I tried:

<DataGridTemplateColumn>
           <DataGrid ItemsSource="{Binding Specifications}"></DataGrid>
 </DataGridTemplateColumn>

But I am getting

can not resolve Specifications in data context


回答1:


Here is a solution with ListBox:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ListBox ItemsSource="{Binding Specifications}"></ListBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>


来源:https://stackoverflow.com/questions/48618302/how-to-bind-collection-to-datagrid-column-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!