【发布时间】:2013-04-29 14:43:06
【问题描述】:
我正在尝试使用我自己的用户控件集合填充WrapPanel。
儿童控制:
<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 控件从 Test 对象中获取 ID 值,而不是从 AllStocks 列表中获取子项。但为什么?我没有定义ItemTemplate 吗?那它有什么用呢?
【问题讨论】:
-
What am I doing wrong?- 一切。 UI 不是存储数据的正确位置。创建一个适当的模型或视图模型来存储该数据并将您的控件绑定到该数据。还要让 UI 创建 UI,不要自己实例化 UI 元素。
标签: wpf data-binding mvvm wrappanel