我正在尝试将一些示例数据加载到我的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>
先谢谢!
答案 0 :(得分:0)
我认为</local:SampleSale >
的末尾应该有SampleSaleData.xaml
。