Silverlight - templated usercontrols and automatic conversion
When changing from manually defining my fields and values in xaml,
to defining a custom/templated usercontrol some of my bindings stopped working.
I created a custom control with two properties,
FeltLabel
and
FeltVerdi
FeltVerdi was defined as a textbox, and in my generic.xaml I had content bound like this:
<Style TargetType="local:DataFelt">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:DataFelt">
<Grid>
….
<TextBox Style="{StaticResource felt}"
Text="{TemplateBinding FeltVerdi />
….
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This worked ok for most of the fields I bound to the ViewModel, but for a couple of fields it seemed as if the binding wasn't working anymore.
After doublechecking my code and bindings I couldn't find anything wrong, so I started to notice that the fields that wouldn't display anymore
were fields defined as numbers in my ViewModel.
It looked like the builtin automatic converters didn't work any more.
I googled it and found a post that helped me, see below.
The solution was to change the binding like this:
<Style TargetType="local:DataFelt">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:DataFelt">
<Grid>
….
<TextBox Style="{StaticResource felt}"
DataContext="{TemplateBinding FeltVerdi}" Text="{Binding}" />
….
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This way automatic conversion stilll works, and my numbers etc is displaying nicely
This post helped me: http://blogs.msdn.com/b/marlat/archive/2009/05/13/silverlight-3-template-binding-vs-relative-binding.aspx
Might be helpful for others?
Rgds
Henri
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment