ListBox DataTemplate绑定问题 [英] ListBox DataTemplate Binding Issue
问题描述
您好,
我在为ListBox定义DataTemplate时遇到了一些问题。我是这样做的:
I'm experiencing some problem in defining a DataTemplate for my ListBox. Here is how I'm doing it:
<ListBox Grid.Row="1" Name="piecesListBox" >
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<Viewbox Height="48" Width="48">
<Canvas Height="{Binding ShapeWidth}" Width="{Binding ShapeLength}" VerticalAlignment="Center" HorizontalAlignment="Center">
<Canvas.Children>
<Path Stroke="Black" StrokeThickness="1">
<Path.Data>
<PathGeometry Figures="{Binding PieceShape}">
</PathGeometry>
</Path.Data>
</Path>
</Canvas.Children>
</Canvas>
</Viewbox>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
我在Visual Studio编辑器中的设计时遇到错误:在"DataTemplate"上抛出了NullReferenceException: Object 引用未设置为对象的实例。
I'm getting an error at design-time in the Visual Studio editor: NullReferenceException was thrown on "DataTemplate": Object reference not set to an instance of an object.
在运行时,一切正常。问题是我无法使用Visual Studio编辑器更改我的 ; UI。
At run-time, everything works fine. The problem is that I cannot use Visual Studio editor do change my UI.
如果我删除 数字 =" { 绑定
PieceShape }" 绑定,
然后我不敏感;没有任何错误。但这不是我需要的。
If I remove the Figures="{Binding PieceShape}" binding, then I don´t get any errors. But this is not what I need.
我对数据模板并不熟悉。我是否必须在XAML中实例化一个对象,以便编辑器能够在设计时加载ListBox?如果是这样,我该怎么办?一个简单的静态资源会有帮助吗?
I'm not much familiar with Data Templates. Do I have to instantiate an object in XAML so the editor can be able to load the ListBox in design time? If so, how can I do that? A simple Static Resource would help?
请注意,
Igor。
推荐答案
为了在设计时加载列表框数据,也许你可以使用ArrayList来创建一个集合数据对象在XAML中使用键或在XAML中使用键实例化集合对象,并将ListBox.ItemsSource引用到该键。
For loading listbox data in design time, perhaps you can use ArrayList to create a collection data object in XAML with a key or instantiate a collection object in XAML with a key, and reference ListBox.ItemsSource to that key.
示例:
< ; Window x:Class =" ListBoxBindingDesignTime.MainWindow"
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "
$
xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:coll =" clr-namespace:System.Collections; assembly = mscorlib"
xmlns:local =" clr-namespace:ListBoxBindingDesignTime"
$
标题= QUOT;主窗口"高度= QUOT; 350"宽度="525">
< Window.Resources>
< coll:ArrayList x:Key =" people">
< local:Person Name =" Tom" />
< local:Person Name =" Ken" />
< local:Person Name =" Jen" />
< / coll:ArrayList>
< local:People x:Key =" peopleColl" />
< /Window.Resources>
< StackPanel>
< ListBox ItemsSource =" {StaticResource people}" DisplayMemberPath =" Name" />
< ListBox ItemsSource =" {StaticResource peopleColl}" DisplayMemberPath =" Name" />
< / StackPanel>
< / Window>
<Window x:Class="ListBoxBindingDesignTime.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:coll="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:local="clr-namespace:ListBoxBindingDesignTime"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<coll:ArrayList x:Key="people">
<local:Person Name="Tom"/>
<local:Person Name="Ken"/>
<local:Person Name="Jen"/>
</coll:ArrayList>
<local:People x:Key="peopleColl"/>
</Window.Resources>
<StackPanel>
<ListBox ItemsSource="{StaticResource people}" DisplayMemberPath="Name"/>
<ListBox ItemsSource="{StaticResource peopleColl}" DisplayMemberPath="Name"/>
</StackPanel>
</Window>
使用System.Windows;
使用System.Collections.Generic;
using System.Windows;
using System.Collections.Generic;
命名空间ListBoxBindingDesignTime
{
public partial class MainWindow:Window
{
public MainWindow()
{
InitializeComponent();
}
}
公共类人员:列表<人员>
{
public People()
{
添加(新人{Name =" Person1"});
添加(新人{Name =" Person2"});
添加(新人{Name =" Person3"});
}
}
公共级人员¥b $ b {
public string Name {set;得到; }
}
}
namespace ListBoxBindingDesignTime
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class People : List<Person>
{
public People()
{
Add(new Person { Name = "Person1" });
Add(new Person { Name = "Person2" });
Add(new Person { Name = "Person3" });
}
}
public class Person
{
public string Name { set; get; }
}
}
这篇关于ListBox DataTemplate绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!