Ateik
Ateik

Reputation: 2586

Binding to UserControl in WinRT

I created a simple Rating user control, the problem this control won't in WinRT work when I use binding, it works fine on windows phone, This is my Control:

public sealed partial class RatingControl : UserControl
{
    public int Rate { get { return (int)GetValue(RateProperty); } set { SetValue(RateProperty, value); } }
    public static readonly DependencyProperty RateProperty = DependencyProperty.Register("Rate",
                                                                    typeof(int),
                                                                    typeof(RatingControl), null);
    public RatingControl()
    {
        this.InitializeComponent();
        this.Loaded += RatingControl_Loaded;
    }

    void RatingControl_Loaded(object sender, RoutedEventArgs e)
    {
        List<Image> Images = new List<Image>();
        for (int i = 0; i < 5; i++)
        {
            Image img = new Image { Width = 35, Height = 35, Margin = new Thickness(3) };
            img.Source = new BitmapImage { UriSource = new System.Uri("ms-appx:Images/Stars/notFilled.png") };
            Images.Add(img);
            sp.Children.Add(img);
        }
        for (int i = 0; i < Rate; i++)
            Images[i].Source = new BitmapImage { UriSource = new System.Uri("ms-appx:Images/Stars/Filled.png") };
    }
}

When I hardcode the value, it works fine:

<local:RatingControl Rate="3" />

but when I use Binding, it just shows zero stars. I checked the value of Rate, it is always zero.

<local:RatingControl Rate="{Binding Decor, Mode=TwoWay}" />

UPDATE: I just found out that the binding happens before I get the value of the Rate, so its zero all the time. How can I fix that? I need the binding to happens after I get the value. Also I thought the Binding happens everytime I change the Rate value.

SOLUTION: I Didnt implement the DependencyObject right, I should've done this:

public static readonly DependencyProperty RateProperty = DependencyProperty.Register("Rate",
                                                                    typeof(int),
                                                                    typeof(RatingControl), new PropertyMetadata(0, new PropertyChangedCallback(BindRateControl)));

Upvotes: 2

Views: 2838

Answers (2)

Ateik
Ateik

Reputation: 2586

SOLUTION: I Didnt implement the DependencyObject right, I should've done this (adding a callback method):

public static readonly DependencyProperty RateProperty = DependencyProperty.Register("Rate",
                                                                typeof(int),
                                                                typeof(RatingControl), 
                                                                new PropertyMetadata(0, new PropertyChangedCallback(BindRateControl)));

Upvotes: 2

LZH
LZH

Reputation: 785

has you try adding the UserControl from code-behind. this help you to ensure that the UserControl is triggered after getting the value.

Upvotes: 0

Related Questions