我正在尝试WrapPanel
用我自己的用户控件集合填充 a 。
儿童控制:
<UserControl x:Class="MyApplication.StockMappings.StockView"
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 Width="140" Height="80">
<Border CornerRadius="6" BorderBrush="Black" BorderThickness="2" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.6*"/>
<RowDefinition Height="0.4*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" FontSize="18" FontWeight="Bold">
<Label Content="{Binding Path=ID}"/>
</TextBlock>
<TextBlock Grid.Row="1" FontSize="12" FontWeight="Bold">
<Label Content="{Binding Path=StockName}"/>
</TextBlock>
</Grid>
</Border>
</Grid>
</UserControl>
cs 文件:
namespace MyApplication.StockMappings
{
/// <summary>
/// Interaction logic for StockView.xaml
/// </summary>
public partial class StockView : UserControl
{
public StockView()
{
InitializeComponent();
}
public string ID
{
get;
set;
}
public string StockName
{
get;
set;
}
}
}
最后,带有环绕面板的窗口:
<Window x:Class="MyApplication.StockMappings.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApplication.StockMappings"
Title="TestWindow" Height="300" Width="300">
<Grid>
<ItemsControl Name="Stocks" ItemsSource="{Binding AllStocks}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:StockView/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
又是 C# 代码:
public partial class TestWindow : Window
{
public TestWindow()
{
InitializeComponent();
var stocks = new[]
{
new StockView() { ID = "M0", StockName = "abc"},
new StockView() { ID = "M1", StockName = "def"},
};
Stocks.DataContext = new Test()
{
AllStocks = stocks.ToList()
};
}
}
类(子数据的Test
容器)非常简单:
public class Test
{
public List<StockView> AllStocks
{
get;
set;
}
}
最后,结果:
所有边框都是空白的。既不ID
也不StockName
显示。
我究竟做错了什么?
我已经确认(通过添加一个虚拟属性)StockView
控件ID
从对象中获取值Test
,而不是从AllStocks
列表中获取子项。但为什么?我没有定义ItemTemplate
吗?那它是为了什么?