一、控件到控件的绑定

1:OneWay
Source影响着Target,但是Target却影响不到Source。
2:OneWayToSource
Target影响Source,而Source却影响不到Target。
3:TwoWay
Source与Target相互影响。
4:OneTime
在OneWay的基础上延伸了一个OneTime,仅绑定一次。如果大家属性Jquery中的one函数我想就可以不用表述了。
  1. <Window x:Class="WpfApplication1.Window1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:sys="clr-namespace:System;assembly=mscorlib"
  5. xmlns:src="clr-namespace:WpfApplication1"
  6. Title="MainWindow" Height="350" Width="525">
  7. <Canvas>
  8. <ScrollBar Height="24" Name="scrollBar1" Width="237" Orientation="Horizontal" Canvas.Left="103" Canvas.Top="51" Minimum="1" Maximum="100" SmallChange="1" />
  9. <Label Canvas.Left="41" Canvas.Top="121" Content="OneWay" Height="28" Name="label1" />
  10. <TextBox Canvas.Left="165" Canvas.Top="121" Height="23"
  11. Text="{Binding ElementName=scrollBar1, Path=Value, Mode=OneWay}"
  12. Name="textBox1" Width="120" />
  13. <Label Canvas.Left="41" Canvas.Top="160" Content="OneWayToSource" Height="28" Name="label2" />
  14. <TextBox Canvas.Left="165" Canvas.Top="160" Height="23"
  15. Text="{Binding ElementName=scrollBar1, Path=Value, Mode=OneWayToSource}"
  16. Name="textBox2" Width="120" />
  17. <Label Canvas.Left="41" Canvas.Top="202" Content="TwoWay" Height="28" Name="label3" />
  18. <TextBox Canvas.Left="165" Canvas.Top="202" Height="23"
  19. Text="{Binding ElementName=scrollBar1, Path=Value, Mode=TwoWay}"
  20. Name="textBox3" Width="120" />
  21. <Label Canvas.Left="41" Canvas.Top="231" Content="OneTime" Height="28" Name="label4" />
  22. <TextBox Canvas.Left="165" Canvas.Top="231" Height="23"
  23. Text="{Binding ElementName=scrollBar1, Path=Value, Mode=OneTime}"
  24. Name="textBox4" Width="120" />
  25. </Canvas>
  26. </Window>

WPF数据绑定 - 图1

二、.NET对象与控件的绑定

  1. <Window x:Class="WpfApplication1.Window1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:sys="clr-namespace:System;assembly=mscorlib"
  5. xmlns:src="clr-namespace:WpfApplication1"
  6. Title="MainWindow" Height="350" Width="525">
  7. <Grid>
  8. <ListView Height="287" HorizontalAlignment="Left" Margin="62,12,0,0" Name="listView1" VerticalAlignment="Top" Width="331">
  9. <ListView.View>
  10. <GridView>
  11. <GridView.Columns>
  12. <GridViewColumn Header="姓名" DisplayMemberBinding="{Binding Name}"/>
  13. <GridViewColumn Header="年龄" DisplayMemberBinding="{Binding Age}"/>
  14. </GridView.Columns>
  15. </GridView>
  16. </ListView.View>
  17. </ListView>
  18. </Grid>
  19. </Window>

WPF数据绑定 - 图2

三、.NET方法与控件的绑定( 在做WPF时,有时我们需要在XAML中绑定.NET中的方法,当然这在实际开发中也是很常用的,不过方法必要由ObjectDataProvider来封装。)

  1. <Window x:Class="WpfApplication1.Window1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:sys="clr-namespace:System;assembly=mscorlib"
  5. xmlns:local="clr-namespace:WpfApplication1"
  6. Title="MainWindow" Height="350" Width="525">
  7. <Window.Resources>
  8. <ObjectDataProvider x:Key="Test" ObjectType="{x:Type local:Student}" MethodName="GetName">
  9. </ObjectDataProvider>
  10. </Window.Resources>
  11. <Grid>
  12. <TextBlock Text="{Binding Source={StaticResource ResourceKey=Test}, Mode=OneWay}"/>
  13. </Grid>
  14. </Window>
  1. using System.Windows.Controls;
  2. using System.Windows.Data;
  3. using System.Windows.Documents;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. using System.Windows.Media.Imaging;
  7. using System.Windows.Shapes;
  8. namespace WpfApplication1
  9. {
  10. /// <summary>
  11. /// Window1.xaml 的交互逻辑
  12. /// </summary>
  13. public partial class Window1 : Window
  14. {
  15. public Window1()
  16. {
  17. InitializeComponent();
  18. }
  19. }
  20. public class Student
  21. {
  22. //前台要引用的方法
  23. public string GetName()
  24. {
  25. return "WPF";
  26. }
  27. }
  28. }

WPF数据绑定 - 图3

四、WPF中的验证

  1. <Window x:Class="WpfApplication1.Window1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:sys="clr-namespace:System;assembly=mscorlib"
  5. xmlns:local="clr-namespace:WpfApplication1"
  6. Title="MainWindow" Height="350" Width="525">
  7. <Window.Resources>
  8. <local:Student x:Key="student"/>
  9. </Window.Resources>
  10. <Grid>
  11. <TextBlock Height="23" HorizontalAlignment="Left" Margin="97,54,0,0" Name="textBlock1" Text="姓名" VerticalAlignment="Top" />
  12. <TextBox DataContext="{StaticResource ResourceKey=student}" Height="23" HorizontalAlignment="Left"
  13. Margin="153,54,0,0" Name="textBox1" VerticalAlignment="Top" Width="120">
  14. <TextBox.Text>
  15. <Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
  16. <!-- 自定义的验证规格,当然可以是多个Check -->
  17. <Binding.ValidationRules>
  18. <local:NameCheck />
  19. </Binding.ValidationRules>
  20. </Binding>
  21. </TextBox.Text>
  22. <TextBox.ToolTip>
  23. <!--将当前的错误信息显示在tooltip上-->
  24. <Binding RelativeSource="{RelativeSource Self}" Path="(Validation.Errors)[0].ErrorContent"/>
  25. </TextBox.ToolTip>
  26. </TextBox>
  27. </Grid>
  28. </Window>
  1. namespace WpfApplication1
  2. {
  3. /// <summary>
  4. /// Window1.xaml 的交互逻辑
  5. /// </summary>
  6. public partial class Window1 : Window
  7. {
  8. public Window1()
  9. {
  10. InitializeComponent();
  11. }
  12. }
  13. public class NameCheck : ValidationRule
  14. {
  15. public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
  16. {
  17. var name = Convert.ToString(value);
  18. //如果名字长度大于4则是非法
  19. if (name.Length > 4)
  20. return new ValidationResult(false, "名字长度不能大于4个长度!");
  21. return ValidationResult.ValidResult;
  22. }
  23. }
  24. public class Student
  25. {
  26. public string Name { get; set; }
  27. public int Age { get; set; }
  28. }
  29. }