我正在尝试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>
先谢谢了!