2

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

这是一个关联了 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 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>

先谢谢了!

4

1 回答 1

0

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

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

于 2012-12-08T21:53:53.857 回答