开发者

Silverlight Binding Question

HI All,

I am trying out my hand with Silverlight for the first time and I have a question regarding binding. I have a form that is bound to a custom data object. On that for I have two boxes labelled as such:

Driving Experiece [Textbox] years [Textbox] months.

I n开发者_高级运维eed to bind this to a single integer property of DrivingExperienceMonths. So for instance, if the DrivingExperienceMonths is equal to 29, I would see 2 in the years textbox and 5 in the months text box.

I can of course add a listener to the text changed events for the text boxes and handle it that way, but everything else on the form is using TwoWay binding, and I was hoping this could as well.

Thanks in Advance


You could do this in WPF by implementing an IMultiValueConverter, but unfortunately, this isn't supported in Silverlight.

The best option is often to have your ViewModel handle this. It could create a "Months" and a "Years" property which automatically synchronizes to your DrivingExperienceMonths value.

If you want to just use OneWay binding (just for display), two IValueConverters could be used. By for TwoWay databinding, it will have to be handled in code.


I would create a value converter (class derived from IValueConverter) that converts the integer to the full string. You would then set an instance of the ValueConverter in the binding.


You can probably create a value converter that extracts the respective info from that property (specified via ConverterParameter)

Edit: As i just noticed (and Copsey noted in his answer) this is not going to work out if you need a two-way binding. Better do conversion in two separate properties...

Something like this:

public int DrivingExperienceMonths { get; set; }

public int ExpMonths
{
    get { return DrivingExperienceMonths % 12; }
    set { DrivingExperienceMonths = (ExpYears * 12) + value; }
}

public int ExpYears
{
    get { return DrivingExperienceMonths / 12; }
    set { DrivingExperienceMonths = (value * 12) + ExpMonths; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜