WPF 基础入门 (Binding 一)

1 通知机制

WPF 中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和目标(Target)。一般情况下,Binding源是逻辑层对象,Binding目标是UI层的控件对象,这样,数据就会通过Binding送达UI层,被UI层展现。后台数据变更后,如何通知到UI层,并更新显示,则通过INotifyPropertyChanged接口实现。

cs 复制代码
public class BaseNotifyProperty:INotifyPropertyChanged
{
	public event PropertyChangedEventHandler PropertyChanged;
	public void OnPropertyChanged(string propertyName="")
	{
		if (PropertyChanged != null)
			PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
	}
}

public string DirFullName
{
   get { return m_DirFullName; }
   set { m_DirFullName = value; OnPropertyChanged("DirFullName"); }
}

2 Binding实现

2.1 绑定(Binding)

现在我有一个Slider和一个Textbox。我希望在滑动Slider的时候,Textbox实时显示Slider的Value属性的值。该怎么办呢?

XAML代码如下:

XML 复制代码
<StackPanel>
	<TextBox x:Name="t1" Text="{Binding Path=Value, ElementName=s1,Mode=TwoWay}"/>
	<Slider x:Name="s1" Minimum="0" Maximum="100" />
</StackPanel>

与上述等价的C#代码为:

cs 复制代码
t1.SetBinding(TextBox.TextProperty, new Binding("Value") { ElementName = "s1" });

还可以这样写:

cs 复制代码
Binding binding = new Binding();
binding.Path = new PropertyPath("Value");
binding.Source = s1;
t1.SetBinding(TextBox.TextProperty, binding);

2.2 多重绑定(MultiBinding)

MultiBinding多个数据源进行组合绑定

XML 复制代码
<TextBlock>
	<TextBlock.Text>
		<MultiBinding StringFormat="{}{0} + {1}">
			<Binding Path="Name" />
			<Binding Path="ID" />
		</MultiBinding>
	</TextBlock.Text>
</TextBlock>

3 绑定转换器

在某种使用场景中,需要对Binding的对象进行转换,如Bool转Visibility,DateTime转String等。

WPF提供了两种Converter接口,单值转换的接口IValueConverter 和多值转换的接口IMultiValueConverter ,它们都属于System.Windows.Data命名空间,在程序集PresentationFramework.dll中。这两种值转换器都是分区域性的。其中方法Convert和ConvertBack都具有指示区域性信息的culture参数。如果区域性信息与转换无关,那么在自定义转换器中可以忽略该参数。

cs 复制代码
public class ProgressValueToAngleConverter : IValueConverter
{
	public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
	{
		if (value != null)
		{     
			double prec=(double)value;
			if(prec<0||prec>100)
				prec=0;
			return 3.6 *prec;
		}
		return 0;
	}

}

public class MultiBindingStringFormatConverter:IMultiValueConverter
{
	public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
	{   
		object result = null;
		if (values != null)
		{  
			var item=values.Last();
			if (item != null)
			{
				string str = item.ToString();
				result = (object)System.String.Format(str, values);
			}
			else
			{
				result = values[0].ToString();
			}
		 
		}
	  return result;
	}
}

5.4 格式化(StringFormat)

XML 复制代码
•	货币格式
<TextBlock Text="{Binding Price, StringFormat={}{0:C}}" />
输出:$123.46
•	货币格式,一位小数
<TextBox Text="{Binding Price, StringFormat={}{0:C1}}" />
输出: $123.5
•	前文字
<TextBox Text="{Binding Price, StringFormat=单价:{0:C}}" /> 
单价:$123.46
•	后文字
<TextBox Text="{Binding Price, StringFormat={}{0}元}" /> 
输出: 123.45678元
•	固定的位数,位数不能少于未格式化前,仅支持整形
<TextBox Text="{Binding Count, StringFormat={}{0:D6}}" /> 
输出: 086723
•	指定小数点后的位数
<TextBox Text="{Binding Total, StringFormat={}{0:F4}}" /> 
输出:28768234.9329
•	用分号隔开的数字,并指定小数点后的位数
<TextBox Text="{Binding Total, StringFormat={}{0:N3}}" />
输出:28,768,234.933
•	格式化百分比
<TextBox Text="{Binding Persent, StringFormat={}{0:P1}}" /> 
输出: 78.9 %
•	占位符
<TextBox Text="{Binding Price, StringFormat={}{0:0000.00}}" /> 
输出: 0123.46
<TextBox Text="{Binding Price, StringFormat={}{0:####.##}}" /> 
输出: 123.46
•	日期/时间
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:d}}" /> 
输出: 5/4/2015
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:D}}" /> 
输出: Monday, May 04, 2015
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:f}}" /> 
输出: Monday, May 04, 2015 5:46 PM
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:F}}" />
输出: Monday, May 04, 2015 5:46:56 PM
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:g}}" />
输出: 5/4/2015 5:46 PM
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:G}}" />
输出: 5/4/2015 5:46:56 PM
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:m}}" /> 
输出: May 04
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:M}}" />
输出: May 04
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:t}}" /> 
输出: 5:46 PM
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:T}}" /> 
输出: 5:46:56 PM
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:yyyy年MM月dd日}}" /> 
输出: 2015年05月04日
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:yyyy-MM-dd}}" />输出: 2015-05-04
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:yyyy-MM-dd HH:mm}}" /> 
输出: 2015-05-04 17:46
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:yyyy-MM-dd HH:mm:ss}}" />
输出: 2015-05-04 17:46:56
•	多重绑定
<TextBox.Text>
<MultiBinding StringFormat="姓名:{0}{1}">
<Binding Path="FristName" />
<Binding Path="LastName" />
</MultiBinding>
</TextBox.Text>
输出: 姓名:AAbb

**************************************************************************************************************

相关推荐
weixin_4640780726 分钟前
wpf加载带材料的3D模型(下载的3D预览一样有纹理)
c#·wpf
暮雪倾风4 小时前
【WPF开发】如何设置窗口背景颜色以及背景图片
ui·wpf
暮雪倾风16 小时前
【WPF开发】超级详细的“文件选择”(附带示例工程)
windows·wpf
明耀20 小时前
WPF RadioButton 绑定boolean值
c#·wpf
暮雪倾风1 天前
【WPF开发】控件介绍-Grid(网格布局)
windows·wpf
芝麻科技2 天前
使用ValueConverters扩展实现枚举控制页面的显示
wpf·prism
笑非不退3 天前
Wpf Image 展示方式 图片处理 显示
开发语言·javascript·wpf
△曉風殘月〆3 天前
在WPF中实现多语言切换的四种方式
wpf·多语言切换
笑非不退3 天前
WPF C# 读写嵌入的资源 JSON PNG JPG JPEG 图片等资源
c#·wpf
He BianGu3 天前
演示:基于WPF的DrawingVisual开发的频谱图和律动图
wpf·示波器·曲线图·频谱分析仪·频谱图·高性能曲线·自绘