views:

50

answers:

4

(Using Silverlight 4.0 and VS 2010)
So I have created a property called Rank in my C# file. How do I now tie that to a control in the UserControl xaml file?

My code: (TopicListItem.xaml.cs)

    #region Rank (DependencyProperty)

    /// <summary> 
    /// Rank 
    /// </summary> 
    public int Rank
    {
        get { return (int)GetValue(RankProperty); }
        set { SetValue(RankProperty, value); }
    }
    public static readonly DependencyProperty RankProperty =
        DependencyProperty.Register("Rank", typeof(int), typeof(TopicListItem),
        new PropertyMetadata(0, new PropertyChangedCallback(OnRankChanged)));

    private static void OnRankChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((TopicListItem)d).OnRankChanged(e);
    }

    protected virtual void OnRankChanged(DependencyPropertyChangedEventArgs e)
    {

    }

    #endregion Rank (DependencyProperty)

I want to do this in my TopicListItem.xaml

...
<Textblock Text="{TemplateBinding Rank}"/>
...

but that doesn't really work.

A: 

probably <Textblock Text="{Binding Rank}"/>.

Femaref
Did not work... `ElementName=...` needed.
WmasterJ
+1  A: 
<UserControl xmlns..... 
    x:Name="myUserControl">

....

<Textblock Text="{Binding Rank,ElementName=myUserControl}"/>

....

</UserControl>

You need to set ElementName to x:Name of UserControl, if x:Name is empty, define one.

Akash Kava
This works fine if the `UserControl` is only ever used as a RootVisual or a navigated page. However if its used as a child control this approach is likely to fail since the `UserControl` name may be re-assigned by the outer xaml.
AnthonyWJones
I havent noticed such things, other way would be to specify relative source as self.
Akash Kava
This didn't work since the name needs to be on the root UIElement within the UserControl not on the UserControl itself.
WmasterJ
On further research and experimentation this did actually work. But not if the `Parent.` path syntax is used. So, it seems that `ElementName` sets where the path should start....
WmasterJ
+1  A: 

You need to use Binding, not TemplateBinding,

Also you might want to look into how to get binding errors reported to you - the very helpful default behaviour in WPF is to leave you guessing about binding problems, but you can actually get lots of useful info if you turn it on.

Will Dean
Thanks will for the tip. HOw can I do this?
WmasterJ
In VS2010 there are options in Tools -> Options -> Debugging -> Output Window. Not sure about earlier versions, but you can turn up the wick in code using PresentationTraceSources - that's probably a good term to Google for.
Will Dean
+1  A: 

If you need to bind a property in a Usercontrol's xaml to a property exposed by the same UserControl then use the following pattern:-

<TextBlock Text="{Binding Parent.Rank, ElementName=LayoutRoot}" />

Note that this makes the assumption that root content element inside the UserControl has been given the name "LayoutRoot".

AnthonyWJones
same as Akash answer?
WmasterJ
Thanks Anthony...this actually worked while Akash didn't so those reading this please see comments on Akash answer for reasoning why his fails. This UserControl was actually a child and part of a list...thanks again :)
WmasterJ
Anthony, I would love to read more about this and specifically what the logic/reason is behind certain things such as `Parent.` and `ElementName=` ...thanks!
WmasterJ
Actually for this particular control it worked without using the `Parent.` in the path. It found it anyway.
WmasterJ