在webform中,如果提到“绑定”二字,相信大家都不会陌生,绑定,让我们的代码更加的简洁优美,在wpf中也存在各种神马的绑定,
当然使用上都是行隔理不隔。
一: 控件到控件的绑定
既然是绑定,那么肯定就有”源对象“和”目标对象“两种状态实体,从图的角度上来说存在三种状态:
确实在wpf中存在这三种模式的对应方式,
1:OneWay
正如图A所说,Source影响着Target,但是Target却影响不到Source。
2:OneWayToSource
也正如图B中所表述的一样,Target影响Source,而Source却影响不到Target。
3:TwoWay
这个也就相当于无向图的边,Source与Target相互影响。
4:OneTime
在OneWay的基础上延伸了一个OneTime,仅绑定一次。如果大家属性Jquery中的one函数我想就可以不用表述了。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas>
<ScrollBar Height="24" Name="scrollBar1" Width="237" Orientation="Horizontal" Canvas.Left="103" Canvas.Top="51" Minimum="1" Maximum="100" SmallChange="1" />
<Label Canvas.Left="41" Canvas.Top="121" Content="OneWay" Height="28" Name="label1" />
<TextBox Canvas.Left="165" Canvas.Top="121" Height="23"
Text="{Binding ElementName=scrollBar1, Path=Value, Mode=OneWay}"
Name="textBox1" Width="120" />
<Label Canvas.Left="41" Canvas.Top="160" Content="OneWayToSource" Height="28" Name="label2" />
<TextBox Canvas.Left="165" Canvas.Top="160" Height="23"
Text="{Binding ElementName=scrollBar1, Path=Value, Mode=OneWayToSource}"
Name="textBox2" Width="120" />
<Label Canvas.Left="41" Canvas.Top="202" Content="TwoWay" Height="28" Name="label3" />
<TextBox Canvas.Left="165" Canvas.Top="202" Height="23"
Text="{Binding ElementName=scrollBar1, Path=Value, Mode=TwoWay}"
Name="textBox3" Width="120" />
<Label Canvas.Left="41" Canvas.Top="231" Content="OneTime" Height="28" Name="label4" />
<TextBox Canvas.Left="165" Canvas.Top="231" Height="23"
Text="{Binding ElementName=scrollBar1, Path=Value, Mode=OneTime}"
Name="textBox4" Width="120" />
</Canvas>
</Window>
最终的结果,还是大家自己拖拖滚动条吧,有图有真相。
二:.net对象与控件的绑定
这种绑定还是经常使用的,在WebForm中我们常用的Eval就是此种绑定,因为俺从数据库里好不容易捞了点数据总要呈现在UI上面吧,
好,不多说,上代码说话。
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListView Height="287" HorizontalAlignment="Left" Margin="62,12,0,0" Name="listView1" VerticalAlignment="Top" Width="331">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="姓名" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="年龄" DisplayMemberBinding="{Binding Age}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
首先谢天谢地,我们的数据出来了,好,现在我们有需求了,我现在需要给奇偶行填充不同底色,并且age=22的这行数据标红,那在wpf中该
怎么做呢?我们依稀的记得在webform中我们会在“行事件”上做手脚,在数据的绑定上wpf给我们提供了一个口子,也就是在绑定时可以插入自
己的”事件处理代码“,但必须要继承自IValueConverter。
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ColorConvert x:Key="myConvert"/>
<Style x:Key="item" TargetType="{x:Type ListViewItem}">
<Setter Property="Background">
<Setter.Value>
<Binding RelativeSource="{RelativeSource Self}"
Converter="{StaticResource myConvert}"/>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ListView ItemContainerStyle="{StaticResource ResourceKey=item}" Height="287" HorizontalAlignment="Left" Margin="62,12,0,0" Name="listView1" VerticalAlignment="Top" Width="331">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="姓名" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="年龄" DisplayMemberBinding="{Binding Age}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Student> list = new List<Student>();
for (int i = 20; i < 30; i++)
{
list.Add(new Student() { Name = "hxc" + i, Age = i });
}
listView1.ItemsSource = list;
}
}
public class ColorConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//这里的value既为当前的行对象
var item = value as ListViewItem;
//获取当前的item在当前的Listview中的位置
var view = ItemsControl.ItemsControlFromItemContainer(item) as ListView;
var index = view.ItemContainerGenerator.IndexFromContainer(item);
//当Age=22是红色标示
if ((view.Items[index] as Student).Age ==22)
return Brushes.Red;
if (index % 2 == 0)
return Brushes.Pink;
else
return Brushes.Blue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
}
快看,效果出来了,这里要稍微解释下IValueConverter的使用步骤:
①:自定义一个类继承自IValueConverter,其中Convert方法的value 为绑定参数,parameter参数为绑定参数的附带值。
1 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
②:我们需要在xaml中引用并写入资源。
<Window.Resources>
<local:ColorConvert x:Key="myConvert"/>
<Style x:Key="item" TargetType="{x:Type ListViewItem}">
<Setter Property="Background">
<Setter.Value>
<Binding RelativeSource="{RelativeSource Self}"
Converter="{StaticResource myConvert}"/>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
③:最后也就是在Binding中使用Convert,wpf在绑定数据的时候会自动调用我们自定义的myConvert方法。
<Window.Resources>
<local:ColorConvert x:Key="myConvert"/>
<Style x:Key="item" TargetType="{x:Type ListViewItem}">
<Setter Property="Background">
<Setter.Value>
<Binding RelativeSource="{RelativeSource Self}"
Converter="{StaticResource myConvert}"/>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
三: .net方法与控件的绑定
在做wpf时,有时我们需要在xaml中绑定.net中的方法,当然这在实际开发中也是很常用的,不过方法必要由ObjectDataProvider来封装。
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication5"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ObjectDataProvider x:Key="Test" ObjectType="{x:Type local:Student}" MethodName="GetName">
</ObjectDataProvider>
</Window.Resources>
<Grid>
<TextBlock Text="{Binding Source={StaticResource ResourceKey=Test}, Mode=OneWay}"/>
</Grid>
</Window>
namespace WpfApplication5
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class Student
{
//前台要引用的方法
public string GetName()
{
return "一线码农";
}
}
}
四:wpf中的验证
我们知道不管在什么体系架构中都有属于自己的一套验证体系,比如webform中的验证控件,mvc中的特性验证,当然wpf也是有的,为了
验证的灵活性,实际开发中我们用的比较多的还是”自定义验证“,其实只需要实现ValidationRule接口就行了,然后写上自定义的验证逻辑。
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Student x:Key="student"/>
</Window.Resources>
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="97,54,0,0" Name="textBlock1" Text="姓名" VerticalAlignment="Top" />
<TextBox DataContext="{StaticResource ResourceKey=student}" Height="23" HorizontalAlignment="Left" Margin="153,54,0,0" Name="textBox1" VerticalAlignment="Top" Width="120">
<TextBox.Text>
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
<!-- 自定义的验证规格,当然可以是多个Check -->
<Binding.ValidationRules>
<local:NameCheck />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
<TextBox.ToolTip>
<!--将当前的错误信息显示在tooltip上-->
<Binding RelativeSource="{RelativeSource Self}" Path="(Validation.Errors)[0].ErrorContent"/>
</TextBox.ToolTip>
</TextBox>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication4
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class NameCheck : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
var name = Convert.ToString(value);
//如果名字长度大于4则是非法
if (name.Length > 4)
return new ValidationResult(false, "名字长度不能大于4个长度!");
return ValidationResult.ValidResult;
}
}
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
}
同样这里也需要注意的就是:
① 实现ValidationRule接口,重写Validate方法,其中的逻辑,你懂的。
1 public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
② 然后我们在需要验证的控件上追加Rule验证, 其中的UpdateSourceTrigger设定为字段改变时触发,当然可选值有很多...
<TextBox.Text>
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
<!-- 自定义的验证规格,当然可以是多个Check -->
<Binding.ValidationRules>
<local:NameCheck />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
③ 最后要将实体写入到验证控件的DataContext上,最后大功告成。
1 <TextBox DataContext="{StaticResource ResourceKey=student}" Height="23" HorizontalAlignment="Left" Margin="153,54,0,0" Name="textBox1" VerticalAlignment="Top" Width="120">