简体   繁体   English

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

[英]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. 我试图将一些示例数据加载到我的UserControl以在设计时工作。

It's a Sale that has associated a collection of OrderItems and a Number: 这是一个与OrderItems和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. 问题是绑定到Number属性的TextBox很好显示,但是绑定到OrderItems集合的DataGrid是空的,没有显示项目。 Why not? 为什么不?

This the SampleLineItem class: 这是SampleLineItem类:

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

And the SampleSaleData.xaml : 还有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:

<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 . 我觉得应该有一个</local:SampleSale >在年底SampleSaleData.xaml

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

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