所以我最终决定从WinForms转移到WPF,并且我的旅程非常有趣。我有一个简单的应用程序,我将ObservableCollection
绑定到ListBox
。绑定到WPF中的集合
我有一个Animal
实体:
namespace MyTestApp
{
public class Animal
{
public string animalName;
public string species;
public Animal()
{
}
public string AnimalName { get { return animalName; } set { animalName = value; } }
public string Species { get { return species; } set { species = value; } }
}
}
而一个AnimalList
实体:
namespace MyTestApp
{
public class AnimalList : ObservableCollection<Animal>
{
public AnimalList() : base()
{
}
}
}
最后,这里是我的主窗口:
<Window x:Class="MyTestApp.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyTestApp"
Title="Window3" Height="478" Width="563">
<Window.Resources>
<local:AnimalList x:Key="animalList">
<local:Animal AnimalName="Dog" Species="Dog"/>
<local:Animal AnimalName="Wolf" Species="Dog"/>
<local:Animal AnimalName="Cat" Species="Cat"/>
</local:AnimalList>
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical" Margin="10,0,0,0">
<TextBlock FontWeight="ExtraBold">List of Animals</TextBlock>
<ListBox ItemsSource="{Binding Source={StaticResource animalList}, Path=AnimalName}"></ListBox>
</StackPanel>
</Grid>
现在,当我运行应用程序,我看到填充了三个项目的列表框:“d”,“O”,而不是“狗”,“G”,“狼”和“猫”:
我有一种强烈的感觉,我在某个地方做了一些愚蠢的事情(AnimalList构造函数可能?),但我无法弄清楚它是什么。任何帮助表示赞赏。
宾果。那就是诀窍。 – PoweredByOrange 2013-04-25 22:54:04