Question

I'm getting all learned up about binding in WPF. I'm having a lot of trouble debugging the parse errors in my xaml, though. Can somebody pretty please tell me what's wrong with this little piece? :

<Border Name="TrackBackground"
    Margin="0"
    CornerRadius="2"                     
    Grid.Row="1"
    Grid.Column="1"
    Background="BlanchedAlmond"
    BorderThickness="1"
    Height="{TemplateBinding Height}">

    <Canvas Name="PART_Track" Background="DarkSalmon" Grid.Row="1" Grid.Column="1">
        <Thumb Name="ThumbKnob" Height="{Binding ElementName=Part_Track, Path=Height, Mode=OneWay}" />
    </Canvas>
</Border>

It's the databinding that breaks. I get an InvalidAttributeValue exception for ThumbKnob.Height when I try to run this. I know I must be missing something fundamental. So fill me in, stackers, and my gratitude will be boundless.


Changing the ElementName didn't help. There must me something else I'm not getting.

I should mention that I'm testing this in Silverlight. The exact message I'm getting out of Internet Explorer is:

XamlParseException: Invalid attribute value for property Height.

This whole thing is inside a ControlTemplate. I'm making a slider control just to teach myself the concepts.

Was it helpful?

Solution

The ElementName property on a Binding is not supported in Silverlight.

You will notice, if you go into the code behind or inspect the Binding object in class explorer, it doesn't have a property named ElementName.

OTHER TIPS

What I usually do to debug databindings, is add a converter where I can set a breakpoint in VS.NET..

so the Binding would be something like this:

{Binding ElementName=PART_Track, Path=Height, Mode=OneWay, Converter={StaticResources DebugConverter}}

Then the converter can be an empty implementation of an IValueConverter, set a breakpoint in the Convert method and see what the Height is being set to...

Don't forget to add your converter to your Resources...

Perhaps the value is NaN ?

Ok, here's the deal:

In silverlight, you can't bind values from one UI element to another declaratively. The only way to do what I was trying to do here would be in the C# code.

I had a reference for this yesterday, but now I guess you'll just have to take my word for it :)

Hmm there might be a substantial difference between WPF en Silverlight on this point..

I seem to have no trouble what so even compiling and running this sample in a WPF window:

<Slider Width="400" Height="20">
    <Slider.Template>
        <ControlTemplate>
            <Border Name="TrackBackground"
                Margin="0"
                CornerRadius="2"                     
                Grid.Row="1"
                Grid.Column="1"
                Background="BlanchedAlmond"
                BorderThickness="1">

                <Canvas x:Name="PART_Track" Background="DarkSalmon" Grid.Row="1" Grid.Column="1">
                    <Thumb Name="ThumbKnob" Height="{Binding ElementName=PART_Track, Path=Height, Mode=OneWay}" />
                </Canvas>
            </Border>
        </ControlTemplate>
    </Slider.Template>
</Slider>

Perhaps Silverlight has fewer properties in the Thumb class...

http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.thumb.aspx

First of all its a matter of casing...

Change

Part_Track
to
PART_Track
which will fix your binding error..

But I do not think that this is what you are trying to do..

You could use a Grid instead of a canvas, and the Thumb will size automatically. Canvas does not really have a height, for it does not really care about the height of its children...

Hope this helps...

Is the Border in a Template btw ?

Because there is no need for TemplateBinding if the border is not located in a Template (either ControlTemplate or DataTemplate)

Silverlight 3 now includes ElementName binding...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top