【问题标题】:Binding to UserControl in WinRT在 WinRT 中绑定到 UserControl
【发布时间】:2012-05-06 08:15:55
【问题描述】:

我创建了一个简单的 Rating 用户控件,当我使用绑定时,这个控件在 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" />

但是当我使用 Binding 时,它只显示零星。我检查了Rate的值,它总是为零。

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

更新:我刚刚发现绑定发生在我获得 Rate 的值之前,所以它一直为零。我该如何解决?我需要在获得值后进行绑定。我还认为每次更改 Rate 值时都会发生绑定。

解决方案:我没有正确实现 DependencyObject,我应该这样做:

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

【问题讨论】:

  • 你的 DataContext 是什么?我认为这可能是由绑定错误引起的。您通常可以在“输出/调试”窗口 (Ctrl+W,O) 中跟踪它们。可能 Decor 的类型不是 int。
  • 我确实检查过,它给出了数字。同样的代码在 wp7 上也能正常工作。
  • 可以分享一个测试项目吗?绑定应该在 InitializeComponent 调用中发生,或者可能在不久之后发生。 Loaded 事件可能会在以后发生。您在哪里更改控件中的速率值?
  • 您能分享您的解决方案吗?我也有类似的问题...
  • 我确实分享了解决方案,您会在我的问题结束时找到它。如果那没有帮助,我将分享整个代码

标签: xaml windows-8 windows-runtime winrt-xaml


【解决方案1】:

解决方案:我没有正确实现 DependencyObject,我应该这样做(添加回调方法):

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

【讨论】:

    【解决方案2】:

    您是否尝试从代码隐藏中添加 UserControl。这可以帮助您确保在获取值后触发 UserControl。

    【讨论】:

      猜你喜欢
      • 2012-10-19
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 2017-02-18
      相关资源
      最近更新 更多