binding
源-目标
binding源是逻辑层的对象
binding目标是UI层的控件对象
怎样才能让一个属性具备通知binding值已经改变的能力呢?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;
namespace StackPanelDemo
{
/// <summary>
/// Window3.xaml 的交互逻辑
/// </summary>
public partial class Window3 : Window
{
Student student;
public Window3()
{
InitializeComponent();
student = new Student();
Binding binding = new Binding();
binding.Source = student;//给binging指定数据源
binding.Path = new PropertyPath("Name");//给binging指定访问路径
BindingOperations.SetBinding(nameTextBox, TextBox.TextProperty,binding);//把数据源和目标连接在一起,第一个参数指定binging的目标,第二个参数指定送达目标的哪个属性,第三个参数指定哪个binging把数据源和目标连接起来
}
private void nameBtn_click(object sender, RoutedEventArgs e)
{
student.Name += "name";
}
}
}
<Window x:Class="StackPanelDemo.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:StackPanelDemo"
mc:Ignorable="d"
Title="Window3" Height="450" Width="800">
<Grid>
<StackPanel>
<TextBox Name="nameTextBox" Margin="10"></TextBox>
<Button Content="show name" Margin="10" Click="nameBtn_click"></Button>
</StackPanel>
</Grid>
</Window>
实例2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;
namespace BindDemo
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
List<Student> students = new List<Student>();
students.Add(new Student() { Id = 1, Name = "Tom", Age = 16 });
students.Add(new Student() { Id = 2, Name = "Tom2", Age = 26 });
students.Add(new Student() { Id = 2, Name = "Tom3", Age = 36 });
//listBox设置binding
aListBox.ItemsSource = students;
aListBox.DisplayMemberPath = "Name";
//为TextBox设置binding
Binding binding = new Binding("SelectedItem.Id") { Source=aListBox};
aTextBox.SetBinding(TextBox.TextProperty, binding);
}
}
}
<Window x:Class="BindDemo.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BindDemo"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<Grid>
<StackPanel>
<TextBlock Text="student id:" Margin="10"></TextBlock>
<TextBox Name="aTextBox" Margin="10"></TextBox>
<TextBlock Text="student list" Margin="10"></TextBlock>
<ListBox Name="aListBox"></ListBox>
</StackPanel>
</Grid>
</Window>
网友评论