Original WPF Resource binding Custom collection class.

Source: Internet
Author: User
Tags mscorlib

A brief introduction to the WPF resource dictionary:

Each WPF interface element has a property named resource, which inherits to the FrameworkElement class, which is of type ResourceDictionary. ResourceDictionary can store resources in the form of key-value pairs, which are used in the form of key-value pairs to obtain resource objects when a resource is to be used. When you save a resource, the ResourceDictionary resource object is an object type, so when you use the resource to type-convert the resource object, the XAML compiler can automatically identify the resource type based on attribute, and throws an exception if the type is not correct.

If the resource dictionary stores a collection type, and the application only wants to bind one of the elements, you will need to write your own converter to return the desired element value.

The following shows an example of an element in a binding collection:

First define the contents of the collection:

1<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"2xmlns:sys="Clr-namespace:system;assembly=mscorlib"3xmlns:syscollection="Clr-namespace:system.collections;assembly=mscorlib"4xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml">5<!--login screen--6<syscollection:arraylist x:key="Page_login">7<syscollection:dictionaryentry key="title"Value="system"/>8<syscollection:dictionaryentry key="Login"Value="Login"/>9<!--Tips-Ten<syscollection:dictionaryentry key="User_isnull"Value="the user cannot be empty"/> One<syscollection:dictionaryentry key="Password_isnull"Value="The password cannot be empty"/> A<syscollection:dictionaryentry key="user_noexist"Value="user does not exist"/> -<syscollection:dictionaryentry key="Password_error"Value="Password Error"/> -</syscollection:ArrayList> the  -</ResourceDictionary>

Next, define the converter:

1 classCultureconverter:ivalueconverter2     {3          Public ObjectConvert (ObjectValue, Type TargetType,Objectparameter, System.Globalization.CultureInfo culture)4         {5             if(Value = =NULL)return NULL;6ArrayList LST = value asArrayList;7             if(LST = =NULL)return NULL;8dictionary<Object,Object> dic = lst. Cast<dictionaryentry> (). ToDictionary (item = Item). Key, item =item. Value);9             if(DIC. ContainsKey (parameter))Ten                 returnDic[parameter]; One             Else A                 return NULL; -         } -  the          Public ObjectConvertback (ObjectValue, Type TargetType,Objectparameter, System.Globalization.CultureInfo culture) -         { -             Throw Newnotimplementedexception (); -         } +}

Finally, reference the resource in XAML:

1<!--define the converter resource--2<Window.Resources>3<local:cultureconverter x:key="Cultureconverter"/>4</Window.Resources>5<!--referencing resources in XAML--6<label content="{Binding Converter={staticresource cultureconverter}, Converterparameter=title,source={staticresource Page_ Login}}"grid.row="1"Verticalalignment="Top"Fontsize=" $"fontweight="Bold"padding="0"grid.columnspan="2"Horizontalcontentalignment="Center"/>

Note: You cannot refer to DynamicResource in converter and source.

Application resource converters can flexibly implement resource references, especially grouped resources. A very good case: the application of internationalization.

Original WPF Resource binding Custom collection class.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.