WPF ListBox自定义类型数据绑定
要准备
1.自定义类型 MyObj(需继承INotifyPropertyChanged)
2.自定义类型集合类 MyObjs(需继承ObservableCollection<MyObj>)
如下:
自定义类型
XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:My"
Title="MainWindow" Height="506" Width="880" Loaded="Window_Loaded">
<Window.Resources>
<local:People x:Key="MyDataSource"></local:People>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{StaticResource MyDataSource}">
<!--<ListBox ItemsSource="{Binding Source={StaticResource MyDataSource}}">这样写也可以-->
</ListBox>
</Grid>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:My"
Title="MainWindow" Height="506" Width="880" Loaded="Window_Loaded">
<Window.Resources>
<local:People x:Key="MyDataSource"></local:People>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{StaticResource MyDataSource}">
<!--<ListBox ItemsSource="{Binding Source={StaticResource MyDataSource}}">这样写也可以-->
</ListBox>
</Grid>
</Window>