简体   繁体   中英

Why DataGrid doesn't display sample design-time data?

I'm trying to load some sample data into my UserControl to work at design time.

It's a Sale that has associated a collection of OrderItems and a Number:

using System;
using System.Collections.ObjectModel;

namespace MiPaladar.SampleData
{
    public class SampleSale
    {
        public int Number { get; set; }        

        public ObservableCollection<SampleLineItem> OrderItems { get; set; }              
    }
}

The problem is the TextBox bound to the Number property shows it just fine, but the DataGrid bound to the collection of OrderItems is empty, not showing the items. Why not?

This the SampleLineItem class:

namespace MiPaladar.SampleData
{
    public class SampleLineItem
    {
        public float Quantity { get; set; }
        public float Price { get; set; }
    }
}

And the SampleSaleData.xaml :

<local:SampleSale xmlns:local="clr-namespace:MiPaladar.SampleData" 
                  Number="78" >
    <local:SampleSale.OrderItems>
    <local:SampleLineItem Quantity="1" Price="3.5"/>
    <local:SampleLineItem Quantity="2" Price="7"/>
    <local:SampleLineItem Quantity="1" Price="8"/>
</local:SampleSale.OrderItems>

Finally my UserControl:

<UserControl x:Class="MiPaladar.Views.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              

         mc:Ignorable="d" 

         d:DesignHeight="300" d:DesignWidth="300" >

<Grid d:DataContext="{d:DesignData /SampleData/SampleSaleData.xaml}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>            
    </Grid.RowDefinitions>

    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Order Number:"  Margin="5"/>
        <TextBox Margin="5" Text="{Binding Number}"/>
    </StackPanel>        

    <!--Orderitems-->
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding OrderItems}"
              Grid.Row="1" Margin="5" >

        <DataGrid.Columns>
            <DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" />

            <DataGridTextColumn Header="Price" Binding="{Binding Price, StringFormat=c}" />

        </DataGrid.Columns>
    </DataGrid>        

</Grid>

Thanks in advanced!

EDIT: never mind -- you beat me to it; although I'm curious if my change would have worked as well.

I think there should be a </local:SampleSale > at the end of SampleSaleData.xaml .

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