为什么DataGrid不显示样本设计时数据?

时间:2012-12-08 20:24:44

标签: c# .net wpf datagrid user-controls

我正在尝试将一些示例数据加载到我的UserControl中以便在设计时工作。

这是一个Sale,它关联了OrderItems的集合和一个数字:

using System;
using System.Collections.ObjectModel;

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

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

问题是绑定到TextBox属性的Number显示它很好,但绑定到DataGrid集合的OrderItems为空,不显示项目。为什么不呢?

这是SampleLineItem类:

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

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>

最后我的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>

先谢谢!

1 个答案:

答案 0 :(得分:0)

编辑:没关系 - 你打败了我;虽然我很好奇我的改变是否也能奏效。

我认为</local:SampleSale >的末尾应该有SampleSaleData.xaml