WPF 数据绑定和控件使用示例

在Windows Presentation Foundation (WPF)中,数据绑定是一个强大的功能,它允许开发者将UI控件与数据源连接起来,从而实现数据的展示和交互。本文将通过一个简单的示例,展示如何在WPF中使用DataGrid控件进行数据绑定,并通过TextBox和DatePicker控件与DataGrid进行交互。

定义示例

首先,定义一个名为Articolo的类,用于表示产品信息。这个类包含三个属性:产品代码(Codice)、产品描述(Descrizione)和过期日期(DataScadenza)。以下是该类的VB.NET实现:

Public Class Articolo Dim _codice As String Dim _descrizione As String Dim _dataScadenza As Date Public ReadOnly Property Codice As String Get Return _codice End Get End Property Public Property Descrizione As String Get Return _descrizione End Get Set(value As String) _descrizione = value End Set End Property Public Property DataScadenza As Date Get Return _dataScadenza End Get Set(value As Date) _dataScadenza = value End Set End Property Public Sub New(codart As String, desart As String) _codice = codart _descrizione = desart _dataScadenza = New Date(Now.Year + 1, Now.Month, Now.Day) End Sub End Class

在类的构造函数中,只请求代码和描述的参数,而过期日期被设置为一年后。Codice属性是只读的,一旦创建后不能更改。

数据绑定到DataGrid

接下来,将创建一个名为MainWindow.xaml的WPF窗口,并在加载事件中初始化产品列表。以下是XAML代码:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ERP_test" x:Class="MainWindow" Title="MainWindow" Height="269" Width="563" Loaded="Window_Loaded"> </Window>

在代码后台,将初始化一个产品列表,并将其绑定到DataGrid控件。以下是VB.NET代码:

Dim prodotti As List(Of Articolo) Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) prodotti = New List(Of Articolo)() prodotti.Add(New Articolo("Prodotto01", "ARTICOLO TEST")) prodotti.Add(New Articolo("Prodotto02", "PROVA")) prodotti.Add(New Articolo("Prodotto03", "NESSUNA DESCRIZIONE")) DataGrid1.ItemsSource = prodotti End Sub

通过设置DataGrid控件的ItemsSource属性,将产品列表绑定到DataGrid。这样,当程序首次运行时,DataGrid将显示产品列表。

控件之间的数据绑定

希望用户能够通过滚动列表来查看产品详细信息。为此,将使用TextBox和DatePicker控件,并在它们与DataGrid之间建立绑定。以下是XAML代码的修改:

<TextBox HorizontalAlignment="Left" Height="23" Margin="445,42,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="181" Text="{Binding SelectedItem.Codice, ElementName=DataGrid1, Mode=OneWay}" /> <TextBox HorizontalAlignment="Left" Height="23" Margin="445,70,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="181" Text="{Binding SelectedItem.Descrizione, ElementName=DataGrid1, Mode=TwoWay}" /> <DatePicker HorizontalAlignment="Left" Margin="445,98,0,0" VerticalAlignment="Top" Width="181" SelectedDate="{Binding SelectedItem.DataScadenza, ElementName=DataGrid1, Mode=TwoWay}" />

在这些控件中,设置了数据绑定的源和模式。例如,对于显示产品代码的TextBox,将其绑定到DataGrid1的SelectedItem.Codice属性,并且设置绑定模式为单向(从DataGrid到TextBox)。对于产品描述和过期日期,使用双向绑定模式,这样修改控件的内容将更新其源属性/控件。

除了使用List(Of Our_Class)来填充DataGrid,还可以直接在XAML中引用类。以下是XAML代码的示例:

<DataGrid Name="DG1" HorizontalAlignment="Left" Height="153" Margin="10,10,0,0" VerticalAlignment="Top" Width="305" xmlns:local="clr-namespace:WinTest"> <DataGrid.Columns> <DataGridTextColumn Header="Codice" Binding="{Binding Codice}" /> <DataGridTextColumn Header="Descrizione" Binding="{Binding Descrizione}" /> <DataGridTextColumn Header="Data di scadenza" Binding="{Binding DataScadenza}" /> </DataGrid.Columns> <local:Articolo Codice="CODE_01" Descrizione="MSDN TEST" DataScadenza="2014-12-01" /> </DataGrid>
沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485