Silverlight - templated usercontrols and automatic conversion

No comments
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

Might be helpful for others?

Rgds
Henri

Posted via email from Henris blogg

No comments :

Post a Comment