A common booltoxxx conversion class in WPF

Source: Internet
Author: User

InWPFProgramIn data binding, it is often controlled by a Boolean value.UIControl status, such as whether to be visible or maximize. HoweverUIThe status attribute types of the control are usually not boolean. For exampleUielement. VisibilityIsVisibilityEnumeration,Window. windowstateIsWindowstateEnumeration. I used to write a lotValueconverterFor exampleBooltovisibilityconverterLA,BooltowindowstateconverterIt's very troublesome. Describes a commonBooltoxxxConversion class, which usesBindingObjectConverterparameterAndFallbackvalueTo convert the Boolean Type and any value.

ConverterparameterWhen binding dataUIUploadValueconverterFor example, to convert a string to an integer through data binding, you can useConverterparameterIn decimal or hexadecimal format. WhileFallbackvalueWhen data binding cannot provide a meaningful value (by returningDependencyproperty. unsetvalueThis value), the data binding engine will useFallbackvalueThe value specified in.

First lookBooltovalueconverterOfSource code:

Public Class Booltovalueconverter:Ivalueconverter

{

# RegionIvalueconverter members

 

Public ObjectConvert (ObjectValue,TypeTargettype,ObjectParameter,CultureinfoCulture)

{

If((Bool) Value)

ReturnParameter;

Else

Return Dependencyproperty. Unsetvalue;

}

 

Public ObjectConvertback (ObjectValue,TypeTargettype,ObjectParameter,CultureinfoCulture)

{

Return Object. Equals (value, parameter );

}

 

# Endregion

}

 

convert function, by judging the bound Boolean value, if it is true ( true ), use binding converterparameter attribute saved value. If the value is false ( false ), dependencyproperty is returned. unsetvalue tells the Data Binding engine to use Binding fallbackvalue of the object. convertback is used to determine UI value of the property corresponding to the object (for example, uielement. visibility ) Is it consistent with the preset value (for example, visibility. visible ) is the same, and the corresponding value is returned. You can also use the Code below to learn booltovalueconverter usage - note the highlighted yellow part:

Booltovaluetestwindow. XAML:

<WindowX:Class= "Manualtestproject. wpflibtest. booltovaluetestwindow"

Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"

Xmlns:X= "Http://schemas.microsoft.com/winfx/2006/xaml"

Xmlns:Converter= "CLR-namespace: wpflib. converters; Assembly = wpflib"

Title= "Booltovaluetestwindow"Height= "300"Width= "300"X:Name= "Layoutroot">

<Window. Resources>

<Converter:BooltovalueconverterX:Key= "Booltovalueconverter"/>

</Window. Resources>

<Stackpanel>

<CheckboxIschecked= "{BindingElementname= Layoutroot,Path= Boolvalue }"

Content= "Check box"/>

<ButtonContent= "True"

Visibility = "{ Binding Elementname = Layoutroot, Path = Boolvalue, Converter = { Staticresource Booltovalueconverter }, Converterparameter = Visible, Fallbackvalue = Collapsed }"

X:Name= "Truebutton"Click= "Truebutton_click"/>

<ButtonContent= "False"

Visibility = "{ Binding Elementname = Layoutroot, Path = Boolvalue, Converter = { Staticresource Booltovalueconverter }, Converterparameter = Collapsed, Fallbackvalue = Visible }"

X:Name= "Falsebutton"Click= "Falsebutton_click"/>

</Stackpanel>

</Window>

 

Booltovaluetestwindow. XAML. CS:

Public Partial Class Booltovaluetestwindow:Window

{

Public BoolBoolvalue

{

Get{Return(Bool) Getvalue (boolvalueproperty );}

Set{Setvalue (boolvalueproperty,Value);}

}

 

Public Static Readonly DependencypropertyBoolvalueproperty =

Dependencyproperty. Register ("Boolvalue",

Typeof(Bool),

Typeof(Booltovaluetestwindow),

New Uipropertymetadata(False));

 

PublicBooltovaluetestwindow ()

{

Initializecomponent ();

}

 

Private VoidTruebutton_click (ObjectSender,RoutedeventargsE)

{

Boolvalue =False;

}

 

Private VoidFalsebutton_click (ObjectSender,RoutedeventargsE)

{

Boolvalue =True;

}

}

 

 

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.