本文基本属于转贴(在More Databinding and Custom Controls基础上做了少量改写,出于学习的目的^_^)

目的:
  在页面上呈现用户列表(显示每个用户的用户名和年龄)
思路:
  定义一个User类,用以描述每个用户;
  定义一个Users类,用以存储多个用户;
  定义一个UserView控件,用以格式化显示每个用户;
  在最终的页面上通过ListBox控件显示用户列表;

以下为各个部分的代码:
User.cs
WPF:数据绑定基础(临摹贴)    public class User

UserView.xaml
WPF:数据绑定基础(临摹贴)    <WrapPanel>
WPF:数据绑定基础(临摹贴)        
<Label>Name:</Label>
WPF:数据绑定基础(临摹贴)        
<Label Name="lblName" Content="{Binding Path=Name}"/>
WPF:数据绑定基础(临摹贴)        
<Label>Age:</Label>
WPF:数据绑定基础(临摹贴)        
<Label Name="lblAge" Content="{Binding Path=Age}"/>
WPF:数据绑定基础(临摹贴)    
</WrapPanel>

Home.xaml
WPF:数据绑定基础(临摹贴)    <Grid x:Name="gridMain">
WPF:数据绑定基础(临摹贴)        
<StackPanel>
WPF:数据绑定基础(临摹贴)            
<Label>UserList:</Label>
WPF:数据绑定基础(临摹贴)            
<ListBox ItemsSource="{Binding Path=UserList}">
WPF:数据绑定基础(临摹贴)                
<ListBox.ItemTemplate>
WPF:数据绑定基础(临摹贴)                    
<DataTemplate DataType="{x:Type kcl:User}">
WPF:数据绑定基础(临摹贴)                        
<kucl:UserView />
WPF:数据绑定基础(临摹贴)                    
</DataTemplate>
WPF:数据绑定基础(临摹贴)                
</ListBox.ItemTemplate>
WPF:数据绑定基础(临摹贴)            
</ListBox>
WPF:数据绑定基础(临摹贴)        
</StackPanel>
WPF:数据绑定基础(临摹贴)    
</Grid>WPF:数据绑定基础(临摹贴)

Home.xaml.cs
WPF:数据绑定基础(临摹贴)        public Home()

WPF中的数据绑定非常有意思,值得深入研究。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-12
  • 2022-12-23
  • 2021-12-31
  • 2021-11-05
  • 2022-02-07
猜你喜欢
  • 2021-09-06
  • 2021-05-23
  • 2022-12-23
  • 2021-09-29
  • 2022-12-23
  • 2021-08-28
  • 2022-02-13
相关资源
相似解决方案