WPF开发工具是一个比较实用的辅助工具。主要为开发人员实现了图形界面处理的一些相关功能。WPF数据绑定非常的好用,下面的例子可以说明这点。#t#
我们在Xaml代码中创建一个ListView,我们设置ListView中的样式绑定为静态绑定。分别定义三个列用来绑定数据ID,Name,Balance
WPF数据绑定代码如下:
- < ListView
- Name="listView"
- ItemContainerStyle="
{StaticResource ItemContStyle}" - ItemsSource="{Binding}"
- >
- < ListView.View>
- < GridView>
- < GridViewColumn Header=
"ID" DisplayMemberBinding=
"{Binding ID}" /> - < GridViewColumn Header=
"Name" DisplayMemberBinding=
"{Binding Name}" /> - < GridViewColumn Header=
"Balance" Width="140"> - < GridViewColumn.CellTemplate>
- < DataTemplate>
- < TextBlock Text=
"{Binding Balance}"
TextAlignment="Right" /> - < /DataTemplate>
- < /GridViewColumn.CellTemplate>
- < /GridViewColumn>
- < /GridView>
- < /ListView.View>
- < /ListView>
我们在资源中增加下列WPF数据绑定应用代码:
- < Style x:Key="ItemContStyle"
TargetType="{x:Type ListViewItem}">- < Style.Resources>
- < LinearGradientBrush x:Key=
"ProfitBrush" StartPoint="0.5,0"
EndPoint="0.5,1">- < GradientStop Offset="0.1"
Color="#AA00CC00" />- < GradientStop Offset="0.8"
Color="#55008800" />- < /LinearGradientBrush>
- < LinearGradientBrush x:Key=
"LossBrush" StartPoint="0.5,0"
EndPoint="0.5,1">- < GradientStop Offset="0.1"
Color="#AACC0000" />- < GradientStop Offset="0.8"
Color="#55880000" />- < /LinearGradientBrush>
- < local:NumberToPolarValue
Converter x:Key="PolarValueConv" />- < /Style.Resources>
- < Setter Property="Horizontal
ContentAlignment" Value="Stretch" />- < Style.Triggers>
- < DataTrigger Binding="{Binding
Balance, Converter={StaticResource
PolarValueConv}}" Value="+1">- < Setter Property="Background"
Value="{StaticResource ProfitBrush}" />- < /DataTrigger>
- < DataTrigger Binding="{Binding
Balance, Converter={StaticResource
PolarValueConv}}" Value="-1">- < Setter Property="Background"
Value="{StaticResource LossBrush}" />- < /DataTrigger>
- < /Style.Triggers>
- < /Style>
在window中增加:
- xmlns:local="clr-names
pace:HighlightingItems
InWPFListView"
在窗体后台代码处增加下列代码:
- public Window1()
- {
- InitializeComponent();
- this.listView.DataContext
= CreateDataTable();- }
- DataTable CreateDataTable()
- {
- DataTable tbl = new
DataTable( "Customers" );- tbl.Columns.Add( "ID",
typeof( int ) );- tbl.Columns.Add(
"Name", typeof( string ) );- tbl.Columns.Add(
"Balance", typeof(
decimal ) );- tbl.Rows.Add( 1,
"John Doe", 100m );- tbl.Rows.Add( 2, "Jane
Dorkenheimer", -209m );- tbl.Rows.Add( 3, "Fred
Porkroomio", 0m );- tbl.Rows.Add( 4, "Mike
Spike", 550m );- tbl.Rows.Add( 5, "Doris
Yakovakovich", 0m );- tbl.Rows.Add( 6, "Boris
Zinkwolf", -25m );- return tbl;
- }
创建一个类,用来管理调用的资源,代码如下:
- [ValueConversion( typeof
( object ), typeof( int ) )]- public class NumberToPolar
ValueConverter : IValueConverter- {
- public object Convert(
- object value, Type targetType,
- object parameter, CultureInfo culture )
- {
- double number = (double)System.
Convert.ChangeType( value,
typeof(double) );- if( number < 0.0 )
- return -1;
- if( number == 0.0 )
- return 0;
- return +1;
- }
- public object ConvertBack(
- object value, Type targetType,
- object parameter, CultureInfo culture )
- {
- throw new NotSupportedException
( "ConvertBack not supported" );- }
- }
以上就是对WPF数据绑定的应用方法介绍。