标签:
1.绑定DataTable 在窗体上显示数据
<Grid> <ListView Name="listview" HorizontalAlignment="Left" Height="426" Margin="19,10,0,0" VerticalAlignment="Top" Width="443"> <ListView.View> <GridView> <GridViewColumn Header="ID" Width="50" DisplayMemberBinding="{Binding ID}"/> <GridViewColumn Header="Name" Width="50" DisplayMemberBinding="{Binding Name}"/> <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}"/> <GridViewColumn Header="Sex" Width="50" DisplayMemberBinding="{Binding Sex}"/> </GridView> </ListView.View> </ListView> </Grid>
namespace WpfApplication2 { /// <summary> /// Window2.xaml 的交互逻辑 /// </summary> public partial class Window2 : Window { public Window2() { InitializeComponent(); DataTable dt = GreatDataTable(); for (int i = 0; i < 10; i++) { DataRow dr = dt.NewRow(); dr[0] = i; dr[1] = "用户" + i; dr[2] = 20+i; dr[3] = "男"; dt.Rows.Add(dr); } this.listview.DataContext = dt; this.listview.SetBinding(ListView.ItemsSourceProperty,new Binding()); } public DataTable GreatDataTable() { DataTable sta = new DataTable("newTable"); DataColumn[] col = new DataColumn[] { new DataColumn("ID"), new DataColumn("Name"), new DataColumn("Age"), new DataColumn("Sex") }; sta.Columns.AddRange(col); return sta; } } }
封装的属性
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace LeiDA { public class DA : INotifyPropertyChanged { private string _Name; public string Name { get { return _Name; } set { _Name = value; OnPropertyChanged("Name"); } } private string _Pwd; public string Pwd { get { return _Pwd; } set { _Pwd = value; OnPropertyChanged("Pwd"); } } public DA() { } public event PropertyChangedEventHandler PropertyChanged; public virtual void OnPropertyChanged(string PropertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); } } } }
2.控件之间的绑定
<Grid> <TextBox HorizontalAlignment="Left" Height="23" Margin="118,94,0,0" TextWrapping="Wrap" Text="{Binding Path=Value,ElementName=slider,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/> <Slider Name="slider" Value="0" HorizontalAlignment="Left" Margin="100,155,0,0" VerticalAlignment="Top" Width="167"/> </Grid>
Text="{Binding Path=Value,ElementName=slider,UpdateSourceTrigger=PropertyChanged}"绑定数据的方式,通过ElementName寻找标签,找到Slider标签,绑定了其中的Value属性,
UpdateSourceTrigger表示双向绑定
运行效果
3.WPF命令
<Grid> <StackPanel Name="spl"> <Button Name="but" Height="30" Content="发送"></Button> <TextBox Name="txtA" Height="60"></TextBox> </StackPanel> </Grid>
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.Shapes; namespace WpfApplication2 { /// <summary> /// Window4.xaml 的交互逻辑 /// </summary> public partial class Window4 : Window { //声明并定义命令 private RoutedCommand rc = new RoutedCommand("Clear", typeof(Window4)); public Window4() { InitializeComponent(); InitializeCommand(); } void InitializeCommand() { //把命令赋值给命令源,并定义快捷键 this.but.Command = rc; this.rc.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt)); //指定命令目标 but.CommandTarget = txtA; //创建命令关联 CommandBinding commandBinding = new CommandBinding() { Command = rc, }; commandBinding.CanExecute += commandBinding_CanExecute; commandBinding.Executed+=commandBinding_Executed; //把命令关联安置在外围控件上 spl.CommandBindings.Add(commandBinding); } //当命令到达目标之后,此方法被调用 private void commandBinding_Executed(object sender, ExecutedRoutedEventArgs e) { txtA.Clear(); //避免事件继续向上传递而降低程序性能 e.Handled = true; } //当探测命令是否可执行的时候该方法会被调用 private void commandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) { if (string.IsNullOrEmpty(txtA.Text)) { e.CanExecute = false; } else { e.CanExecute = true; } e.Handled = true; } } }
运行程序,在TextBox中输入内容之后,Button在命令可执行状态下变为可用,此时单击按钮或者按Alt+C,TextBox就会被清空。运行效果如图:
4.前台改变数值,后台相应改变
封装属性借用上面的DA类
<Grid> <TextBox Name="pw" HorizontalAlignment="Left" Height="23" Margin="69,73,0,0" TextWrapping="Wrap" Text="{Binding Pwd}" VerticalAlignment="Top" Width="120"/> <TextBox HorizontalAlignment="Left" Height="23" Margin="69,136,0,0" TextWrapping="Wrap" Text="{Binding Text, ElementName=pw, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/> <Label Content="{Binding Name,Mode=TwoWay}" HorizontalAlignment="Left" Margin="69,39,0,0" VerticalAlignment="Top"/> <Button Content="按钮" HorizontalAlignment="Left" Margin="69,195,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/> </Grid>
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.Shapes; using LeiDA; namespace WpfApplication2 { /// <summary> /// Window1.xaml 的交互逻辑 /// </summary> public partial class Window1 : Window { DA da; public Window1() { InitializeComponent(); da = new DA() {Name="adimin",Pwd="123" }; this.DataContext = da; } private void Button_Click_1(object sender, RoutedEventArgs e) { da.Name = DateTime.Now.ToString("HH:mm:ss:fff"); da.Pwd = DateTime.Now.ToString("HH:mm:ss"); } } }
运行效果:
点击按钮后
标签:
原文地址:http://www.cnblogs.com/happinesshappy/p/4609197.html