- 浏览: 36026 次
- 性别:
- 来自: 北京
文章分类
最新评论
deeper into data binding
advanced data binding handling
BindingOperations.SetBinding方法
Binding headerBinding = new Binding(presenter.TabHeaderPath);
SetBinding takes three parameters.
The first is the target of the data binding. The second is the dependency property on the target that we are binding to. (Recall that dependency properties are available as static members on their parent class.) The third parameter is the binding we just created,which is the BindingBase object that describes the binding.
communicate changes to the binding
(1)Dependency properties,WPF controls all make use of the dependency property system. You can use dependency properties in your own classes, but often it will be overkill to do so;
(2)INotifyPropertyChanged,Classes that implement this interface raise an event when one of their properties changes. This is fairly lightweight compared to implementing dependency properties.
(3)Event naming convention,If neither of the preceding methods are used, WPF automatically looks for events that follow a naming convention. The convention is the name of the property with the suffix Changed.
GridView,display our collection items in a tabular format;
data template,a way
to describe how data should be visualized in terms of UI elements;
a composition of UI elements;
many controls have properties of type DataTemplate. For example,ItemsControl.ItemTemplate and GridView.CellTemplate;
style, data template,control template
Styles are the simplest of the three, so if you are able to achieve what you want using styles, that is the best choice. Keep in mind that styles allow you to set nonvisual properties as well.
Control templates define the UI elements that compose a given control.That’s a lot more complicated than merely setting some properties. You should use control templates only when you really need to.
Data templates resemble control templates in that they allow you to compose UI elements. They are often used with list controls to define how items in a list should be rendered.
(It’s good practice to store all three in your application’s resources. This helps reduce noise and makes your markup more readable. Additionally, it is a common practice to set control templates using styles)
Formatting Bound Data-converter,
converter are classes that implement IValueConverter,the interface has two methods, Convert and ConvertBack(WPF does not provide any implementations of IValueConverter).
Convert is used when data flows from the source to the target. ConvertBack is used when data flows back to the source (in a two-way binding).)
Both methods take four parameters-
first is value, and it is the actual data to be manipulated;
second is the type of the target data;
third is for general use and can be used to parameterize your converter;
fourth is the cultural information about the executing context.
Binding class has a property called Converter that accepts IValueConverter,we can use it to make hooking.
//PhoneConverter.cs
using System;
using System.Globalization;
using System.Windows.Data;
namespace ContactManager.Presenters
{
public class PhoneConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string result = value as string;
if (!string.IsNullOrEmpty(result))
{
string filteredResult = FilterNonNumeric(result);
long theNumber = System.Convert.ToInt64(filteredResult);
switch (filteredResult.Length)
{
case 11:
result = string.Format("{0:+# (###) ###-####}", theNumber);
break;
case 10:
result = string.Format("{0:(###) ###-####}", theNumber);
break;
case 7:
result = string.Format("{0:###-####}", theNumber);
break;
}
}
return result;
}
private static string FilterNonNumeric(string stringToFilter)
{
if (string.IsNullOrEmpty(stringToFilter)) return string.Empty;
string filteredResult = string.Empty;
foreach (char c in stringToFilter)
{
if (Char.IsDigit(c))
filteredResult += c;
}
return filteredResult;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return FilterNonNumeric(value as string);
}
}
}
在App.xaml中,在<ResourceDictionary>范围内增加
<Presenters:PhoneConverter x:Key="phoneConverter" />
在ContactListView.xaml中修改
<GridViewColumn Header="Last Name"
DisplayMemberBinding="{Binding LastName}" />
<GridViewColumn Header="First Name"
DisplayMemberBinding="{Binding FirstName}" />
<GridViewColumn Header="Work Phone"
DisplayMemberBinding="{Binding Path=OfficePhone, Converter={StaticResource phoneConverter}}" />
<GridViewColumn Header="Cell Phone"
DisplayMemberBinding="{Binding Path=CellPhone, Converter={StaticResource phoneConverter}}" />
<GridViewColumn Header="Email Address"
DisplayMemberBinding="{Binding PrimaryEmail}" />
对于反向的实现,则需修改EditContactView.xaml
Parameterizing Converters
{Binding Path=OfficePhone,
Converter={StaticResource phoneConverter},
ConverterParameter=’TEL:{0}’}
该标记等效于string.Format(“TEL:{0}”, OfficePhone);
collection view
collection view出现的根源留作问题(与ObservableCollection<Contact>有关,与解耦有关)
The CollectionView class is part of WPF and acts as a wrapper around the collection we are interested in manipulating;
The collection view tracks additional information about the collection, such as sorting, filtering, and grouping currently selected items;
WPF automatically uses a collection view whenever we bind to a collection;
It’s important to understand that a collection view does not change the underlying collection (This means that you can have multiple views of the same collection and they don’t interfere with one another. Two ListBox controls bound to the same collection track their currently selected items independently).
CollectionView | The default view for collections that only implement IEnumberable |
ListCollectionView | Used for collections that implement IList |
BindingListCollectionView | For collections implementing IBindingList |
在XAML中使用collection view时,必须通过代理类型CollectionViewSource;a proxy that allows us to declare a collection view in markup.property-
-View,to access the collection view;
-Source,explicitly assign to the collection we are interested in;
<UserControl.Resources>
<CollectionViewSource x:Key=”contactSource”
Source=”{Binding AllContacts}”>
<CollectionViewSource.SortDescriptions>
<ComponentModel:SortDescription PropertyName=”LookupName” />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</UserControl.Resources>
修改相关实例的绑定,
{Binding Source={StaticResource contactSource}}
发表评论
-
wpf应用实例
2010-07-24 15:45 1065使用WPF快速创建可拖拽的对象和窗体。 -
业务流的前端,intel终极理想:感应终端环境与任意平面成屏技术
2010-07-08 12:44 1021在欧美等人力成本高 ... -
wpf 研习1-24小时自学wpf16
2010-06-15 23:21 1382Visualizing Lists-displaying mo ... -
wpf 研习1-24小时自学wpf14
2010-06-13 10:27 1400Resources and Styles 从下图,我们 ... -
wpf 研习1-24小时自学wpf13
2010-06-11 23:04 909Presenters and Views-the Shell ... -
wpf 研习1-24小时自学wpf12
2010-06-11 14:26 769A Contact Manager Choosin ... -
wpf 研习1-24小时自学wpf11
2010-06-10 15:02 969output WPF Document Con ... -
了解WPF中的路由事件和命令
2010-06-09 23:12 1332路由事件浏览 (1)在vs designer中,如在窗口中增 ... -
wpf 研习1-24小时自学wpf10
2010-06-09 16:25 976Commands In WPF, a command is ... -
wpf 研习1-24小时自学wpf9
2010-06-08 19:19 773events handle routed event: ... -
wpf 研习1-24小时自学wpf8
2010-06-07 16:10 886A real-world program basic ... -
wpf 研习1-24小时自学wpf7
2010-06-07 11:04 846Application deployable file ... -
wpf 研习1-24小时自学wpf6
2010-06-05 17:14 873Data Binding markup extensi ... -
wpf 研习1-24小时自学wpf5
2010-06-05 16:14 1064basic Control Control base ... -
wpf 研习1-24小时自学wpf4
2010-06-05 11:31 666Layout Panel,Decorator; Sy ... -
wpf 研习1-24小时自学wpf3
2010-06-04 19:11 812wpf项目文件 vs->新项目->wpf应用程序 ... -
wpf 研习1-24小时自学wpf2
2010-06-04 15:39 829XAML,既然用户控件是一个大类,那么干脆对这个集合进行统一的 ... -
wpf 研习1-24小时自学wpf1
2010-06-04 12:05 727WPF是微软.net的UI 架构,也是非常重要的框架。 ... -
wpf主要知识点
2010-06-04 10:47 982template,presenter,layout,visua ... -
路由事件
2010-05-17 22:09 648如果我们在一个窗口上增加一个按钮,并且看下它的后置代码,会发现 ...
相关推荐
本文将围绕“wpf 研习1-24小时自学wpf6”这一主题,探讨WPF的核心概念和技术,旨在帮助初学者快速上手并深入理解这一强大的UI框架。 WPF是微软.NET Framework的一部分,它提供了全面的图形系统,包括2D和3D渲染、...
【标题】:“WPF研习1-24小时自学WPF9” 在Windows Presentation Foundation(WPF)的世界里,这是一段深入学习之旅的开端。WPF是.NET Framework的一个重要组成部分,它为创建丰富的、高性能的桌面应用程序提供了...
标题 "wpf 研习1-24小时自学wpf7" 提示我们这是一个关于Windows Presentation Foundation(WPF)的学习资源,可能是书籍、课程或教程的一部分,旨在帮助初学者在24小时内掌握WPF的基础知识。WPF是.NET Framework中的...
【标题】:“wpf 研习1-24小时自学wpf8”是指一系列针对Windows Presentation Foundation(WPF)框架的学习教程,旨在帮助初学者在24小时内掌握WPF的基础知识和应用技巧。WPF是.NET Framework的一部分,用于构建...
NULL 博文链接:https://x-dome.iteye.com/blog/684355
对于想要开发Windows应用的学员,WinForms和WPF(Windows Presentation Foundation)的使用也会有所介绍。 在幻灯片部分,可能会包含每章节的重点摘要,清晰的示例图解,以及关键概念的解释,帮助学生更好地吸收和...