Jan Slodicka
Jan Slodicka

Reputation: 1515

Setting char properties in Xaml

Perhaps it is something trivial but I am out of ideas...

Originally I wanted to add some features to PasswordBox. Because it is a sealed class, original properties have to be replicated, among them PasswordChar. Looks trivial, but when I started to set PasswordChar in Xaml, I could not get rid of parser exception.

At the end I simply defined a new property

public char MyProperty {get; set; }

and tried to set it in Xaml as follows:

<MyPasswordBox MaxLength="3" Password="xxx" MyProperty="c" />

I am getting an exception with the call stack looking like

at MS.Internal.XcpImports.CheckHResult()
at MS.Internal.XcpImports.ConvertStringToTypedCValue()
at MS.Internal.SilverlightTypeConverter.ConvertFrom()
at MS.Internal.FrameworkCallbacks.ConvertValueToPropertyType()
....
at MS.Internal.FrameworkCallbacks.SetValueToProperty()
at MS.Internal.FrameworkCallbacks.SetPropertyAttribute()
....
at System.Windows.Application.LoadComponent()
....

As far I can read it, the type conversion string -> char fails. Note that whenever I'll change the type of MyProperty to string (for example), everything works.

Does anybody know how to implement char properties so that they can be set from Xaml?

Working on Windows Phone 7, perhaps that's the problem. (Limited SVL 3)

Upvotes: 2

Views: 873

Answers (1)

Nigel Sampson
Nigel Sampson

Reputation: 10609

I can't verify this will work, but you can give it a go. The long form xaml syntax should work ok.

Add the following to your namespace imports

xmlns:sys="clr-namespace:System;assembly=mscorlib"

Then the following should work

<MyPasswordBox MaxLength="3" Password="xxx">
    <MyPasswordBox.MyProperty>
        <sys:Char>c</sys:Char>
    </MyPasswordBox.MyProperty>
</MyPasswordBox>   

The other solution is to look into type converters to apply to your property so that it'll convert the string for you. Type Convereters and XAML.

Upvotes: 1

Related Questions