WPF 基础入门 (Binding 一)

1 通知机制

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

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代码如下:

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

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

t1.SetBinding(TextBox.TextProperty, new Binding("Value") { ElementName = "s1" });

    还可以这样写:

Binding binding = new Binding();
binding.Path = new PropertyPath("Value");
binding.Source = s1;
t1.SetBinding(TextBox.TextProperty, binding);

2.2  多重绑定(MultiBinding)

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

<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中。这两种值转换器都是分区域性的。其中方法ConvertConvertBack都具有指示区域性信息的culture参数。如果区域性信息与转换无关,那么在自定义转换器中可以忽略该参数。

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)

•	货币格式
<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

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

相关推荐

  1. WPF 基础入门Binding

    2024-01-01 15:32:02       39 阅读
  2. WPF 基础Binding 二)

    2024-01-01 15:32:02       30 阅读
  3. WPF 基础入门 (触发器)

    2024-01-01 15:32:02       40 阅读
  4. WPF 基础入门(样式)

    2024-01-01 15:32:02       40 阅读
  5. C# WPF入门学习(

    2024-01-01 15:32:02       8 阅读
  6. WPF 基础入门(XAML理解二)

    2024-01-01 15:32:02       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-01 15:32:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-01 15:32:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-01 15:32:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-01 15:32:02       20 阅读

热门阅读

  1. linux的页缓存page cache

    2024-01-01 15:32:02       39 阅读
  2. C语言——float.h文件

    2024-01-01 15:32:02       34 阅读
  3. 二叉树 经典例题

    2024-01-01 15:32:02       32 阅读
  4. c# 等值线算法

    2024-01-01 15:32:02       39 阅读
  5. 算法的空间复杂度

    2024-01-01 15:32:02       32 阅读
  6. Android 配置不同应用ID

    2024-01-01 15:32:02       41 阅读
  7. 算法练习Day26 (Leetcode/Python-贪心算法)

    2024-01-01 15:32:02       42 阅读
  8. 分支和循环的综合作业

    2024-01-01 15:32:02       39 阅读
  9. 【Git】git基础

    2024-01-01 15:32:02       34 阅读
  10. Linux环境安装1

    2024-01-01 15:32:02       36 阅读
  11. PathManager功能

    2024-01-01 15:32:02       37 阅读
  12. xinference

    2024-01-01 15:32:02       47 阅读
  13. docker 部署mysql

    2024-01-01 15:32:02       41 阅读