2

私は単純な評価ユーザー コントロールを作成しました。バインディングを使用するとこのコントロールが WinRT で機能しないという問題があり、Windows phone で正常に動作します。これが私のコントロールです。

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") };
    }
}

値をハードコーディングすると、正常に機能します。

<local:RatingControl Rate="3" />

しかし、バインディングを使用すると、星がゼロしか表示されません。Rate の値を確認しましたが、常にゼロです。

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

更新:レートの値を取得する前にバインディングが発生することがわかったので、常にゼロです。どうすれば修正できますか?値を取得した後にバインディングが発生する必要があります。また、レート値を変更するたびにバインディングが発生すると思いました。

解決策: 私は DependencyObject を正しく実装していませんでした。これを行うべきでした:

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

2 に答える 2

2

解決策:DependencyObjectの権利を実装しなかったので、これを実行する必要がありました(コールバックメソッドを追加):

public static readonly DependencyProperty RateProperty = DependencyProperty.Register("Rate",
                                                                typeof(int),
                                                                typeof(RatingControl), 
                                                                new PropertyMetadata(0, new PropertyChangedCallback(BindRateControl)));
于 2012-05-12T06:11:19.100 に答える
0

コード ビハインドから UserControl を追加してみてください。これにより、値を取得した後に UserControl が確実にトリガーされるようになります。

于 2012-05-11T16:39:51.750 に答える