当前位置:网站首页>WPF 数据绑定(二)
WPF 数据绑定(二)
2022-06-09 06:51:00 【flysh05】
本例演示如何将数据模型的实例绑定到界面控件。
XAML代码如下:
<Page x:Class="WpfApp.Page1" 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" xmlns:local="clr-namespace:WpfApp" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" Title="Page1-Binding to Product Object">
<Grid Background="LightGreen">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<!-- Row 1-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Margin="7">Product ID:</TextBlock>
<TextBox Name="txtID" Margin="5" Grid.Column="1">356</TextBox>
<Button x:Name="cmdGetProduct" Click="cmdGetProduct_Click" Margin="5" Padding="2" Grid.Column="2">Get Product</Button>
</Grid>
<!-- Row 2-->
<Border Grid.Row="1" Padding="7" Margin="7" Background="LightSteelBlue">
<Grid Name="gridProductDetails">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Margin="7">Model Number:</TextBlock>
<TextBox Margin="5" Grid.Column="1" Text="{Binding Path=ModelNumber}"></TextBox>
<TextBlock Margin="7" Grid.Row="1">Model Name:</TextBlock>
<TextBox Margin="5" Grid.Row="1" Grid.Column="1" Text="{Binding Path=ModelName}"></TextBox>
<TextBlock Margin="7" Grid.Row="2">Unit Cost:</TextBlock>
<TextBox Margin="5" Grid.Row="2" Grid.Column="1" Text="{Binding Path=UnitCost}"></TextBox>
<TextBlock Margin="7,7,7,0" Grid.Row="3">Description:</TextBlock>
<TextBox Margin="7" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" VerticalScrollBarVisibility="Visible" TextWrapping="Wrap" Text="{Binding Path=Description}"></TextBox>
</Grid>
</Border>
</Grid>
</Page>
后台代码:
/// <summary>
/// Page1.xaml 的交互逻辑
/// </summary>
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
private void cmdGetProduct_Click(object sender, RoutedEventArgs e)
{
int ID;
if (Int32.TryParse(txtID.Text, out ID))
{
try
{
//绑定到数据源
gridProductDetails.DataContext = App.StoreDb.GetProduct(ID);
}
catch (Exception)
{
MessageBox.Show("Error contacting database.");
}
}
else
{
MessageBox.Show("Invalid ID.");
}
}
}
分析:
实现主要方式是从数据查询数据,返回一个数据模型对象,将对象绑定到Grid,也即使代码:
gridProductDetails.DataContext = App.StoreDb.GetProduct(ID);
Grid内的文本控件都可以绑定到模型对象的属性字段,如:
<TextBox Margin="5" Grid.Column="1" Text="{Binding Path=ModelNumber}"></TextBox>
将文本绑定到模型的ModelNumber属性字段,当输入不同ID,获取的不同的对象时,这些绑定文本跟随动态变化。
效果如下:
不同ID查询的对象不同,绑定的属性值也就不同。


边栏推荐
猜你喜欢

Matlab: difference between tf2zp and tf2zpk

209. minimum length subarray

Database operation statement

数学很差能学机器学习吗?

The original, skillful and vulgar skills of cloud

257. 二叉树的所有路径

What should Parker hydraulic motor pay attention to?

The Sandbox 和 Ikonia 达成合作,在元宇宙中还原足球传奇人物皮尔洛的壁画

Quanzhi platform BSP tailoring (4) kernel tailoring --file Systems & Driver & miscellaneous tailoring

QT: 手工布局并关联QSpin和QSlider
随机推荐
不懂数学可以使用机器学习编程吗?
QT---创建对话框1:QDialog的子类查找关键字对话框的实现
Chapter_ 01 mat: basic image container
parker液压马达要注意哪些问题?
傅里叶变换-信号处理
Application and analysis of special cases of data visualization
pycharm 和mysql做学生信息管理系统问题求解
Multithreading Basics (II)
For an experienced software engineer, what would be a preferred new programming language to learn?
UML series articles (19) basic behavior - interaction diagram
Top view all processes
The Sandbox 和 Ikonia 达成合作,在元宇宙中还原足球传奇人物皮尔洛的壁画
穆格伺服阀如何存放?简单的几个方法
top查看全部进程
YOLOv4解析 | 第二篇:用YOLOv4训练自己的数据集(超级详细完整版)
UML系列文章(23)高级行为---进程和线程
UML系列文章(27)体系结构建模---部署
当内卷风波及代码领域,看Alluxio将会采取怎样的块分配策略
【快捷记录,方便CV】TeachYourselfSQL_MySQL
MySQL of quantitative transactions - MySQL database events