`

wpf 研习1-24小时自学wpf15

阅读更多

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).

 

Three Types of Collection Views in WPF
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}} 

 

 

  • 大小: 100 KB
  • 大小: 105.3 KB
分享到:
评论

相关推荐

    wpf 研习1-24小时自学wpf6

    本文将围绕“wpf 研习1-24小时自学wpf6”这一主题,探讨WPF的核心概念和技术,旨在帮助初学者快速上手并深入理解这一强大的UI框架。 WPF是微软.NET Framework的一部分,它提供了全面的图形系统,包括2D和3D渲染、...

    wpf 研习1-24小时自学wpf9

    【标题】:“WPF研习1-24小时自学WPF9” 在Windows Presentation Foundation(WPF)的世界里,这是一段深入学习之旅的开端。WPF是.NET Framework的一个重要组成部分,它为创建丰富的、高性能的桌面应用程序提供了...

    wpf 研习1-24小时自学wpf7

    标题 "wpf 研习1-24小时自学wpf7" 提示我们这是一个关于Windows Presentation Foundation(WPF)的学习资源,可能是书籍、课程或教程的一部分,旨在帮助初学者在24小时内掌握WPF的基础知识。WPF是.NET Framework中的...

    wpf 研习1-24小时自学wpf8

    【标题】:“wpf 研习1-24小时自学wpf8”是指一系列针对Windows Presentation Foundation(WPF)框架的学习教程,旨在帮助初学者在24小时内掌握WPF的基础知识和应用技巧。WPF是.NET Framework的一部分,用于构建...

    wpf 研习1-24小时自学wpf3

    NULL 博文链接:https://x-dome.iteye.com/blog/684355

    C#自学最好的课件资源

    对于想要开发Windows应用的学员,WinForms和WPF(Windows Presentation Foundation)的使用也会有所介绍。 在幻灯片部分,可能会包含每章节的重点摘要,清晰的示例图解,以及关键概念的解释,帮助学生更好地吸收和...

Global site tag (gtag.js) - Google Analytics