Xaml 在WinRT中绑定到UserControl
我创建了一个简单的分级用户控件,当我使用绑定时,该控件在WinRT中无法工作,它在windows phone上运行良好,这是我的控件:Xaml 在WinRT中绑定到UserControl,xaml,windows-8,windows-runtime,winrt-xaml,Xaml,Windows 8,Windows Runtime,Winrt Xaml,我创建了一个简单的分级用户控件,当我使用绑定时,该控件在WinRT中无法工作,它在windows phone上运行良好,这是我的控件: public sealed partial class RatingControl : UserControl { public int Rate { get { return (int)GetValue(RateProperty); } set { SetValue(RateProperty, value); } } public static
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") };
}
}
您是否已尝试从代码隐藏添加UserControl。这有助于确保在获取值后触发UserControl。解决方案:我没有正确实现DependencyObject,我应该这样做(添加回调方法):
你的数据上下文是什么?我认为这可能是由绑定错误引起的。您通常可以在输出/调试窗口(Ctrl+W,O)中跟踪它们。也许装饰的类型不是int。我检查过了,它给出了数字。同样的代码在wp7上也可以正常工作。您可以共享一个测试项目吗?绑定应该发生在InitializeComponent调用中,或者可能很快发生。加载的事件可能稍后发生。您在哪里更改控件中的速率值?您能分享您的解决方案吗?我有一个类似的问题…我确实分享了解决方案,你会在我的问题结束时找到它。如果这没有帮助,我将分享整个代码
<local:RatingControl Rate="3" />
<local:RatingControl Rate="{Binding Decor, Mode=TwoWay}" />
public static readonly DependencyProperty RateProperty = DependencyProperty.Register("Rate",
typeof(int),
typeof(RatingControl), new PropertyMetadata(0, new PropertyChangedCallback(BindRateControl)));
public static readonly DependencyProperty RateProperty = DependencyProperty.Register("Rate",
typeof(int),
typeof(RatingControl),
new PropertyMetadata(0, new PropertyChangedCallback(BindRateControl)));