Windows Phone - 依赖属性
我正在尝试让 UserControl 在 Windows Phone 7 中工作。我有一些想要绑定的属性,但无论我是否将它们添加为 DependencyProperties,它们都不会填充。我可以让它们工作的唯一方法是设置 DataContext。我尝试过的代码是(对于一个属性):
public static readonly DependencyProperty MaximumItemsProperty = DependencyProperty.Register("MaximumItems", typeof(int), typeof(ManageIngredientsControl), new PropertyMetadata(0));
/// <summary>
/// Gets or sets the maximum number of items to match.
/// </summary>
/// <value>The maximum number of items to match.</value>
public int MaximumItems
{
get { return Convert.ToInt32(base.GetValue(MaximumItemsProperty)); }
set { base.SetValue(MaximumItemsProperty, value); }
}
<TextBox Grid.Row="1" Grid.Column="1" x:Name="nudMaxIngredients" Width="120" Text="{Binding MaximumItems,Mode=TwoWay,ElementName=root}" InputScope="Number" />
根 UserControl 元素称为“root”,但未填充该值。让它一半工作的唯一方法是使用这个:
public int MaximumItems
{
get { return Convert.ToInt32(DataContext) }
set { DataContext = value; }
}
似乎有什么东西干扰了 DataContext,但如果我绑定到 DependencyProperties 为什么它很重要?
I'm trying to get a UserControl working in Windows Phone 7. I have a few properties which I'd like to bind to, yet they aren't populated regardless of whether I add them as DependencyProperties or not. The only way I can get them to work is by setting the DataContext instead. The code I've tried is (for one property):
public static readonly DependencyProperty MaximumItemsProperty = DependencyProperty.Register("MaximumItems", typeof(int), typeof(ManageIngredientsControl), new PropertyMetadata(0));
/// <summary>
/// Gets or sets the maximum number of items to match.
/// </summary>
/// <value>The maximum number of items to match.</value>
public int MaximumItems
{
get { return Convert.ToInt32(base.GetValue(MaximumItemsProperty)); }
set { base.SetValue(MaximumItemsProperty, value); }
}
<TextBox Grid.Row="1" Grid.Column="1" x:Name="nudMaxIngredients" Width="120" Text="{Binding MaximumItems,Mode=TwoWay,ElementName=root}" InputScope="Number" />
The root UserControl element is called 'root', but the value isn't populated. The only way to get it half working is by using this:
public int MaximumItems
{
get { return Convert.ToInt32(DataContext) }
set { DataContext = value; }
}
It seems something's interfering with the DataContext, but if I'm binding to DependencyProperties why would it matter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜您的 TextBox 位于您的 UserControl 内。如果是这样,则 ElementName 绑定存在问题,如所述 在这里。
基本上,您在 XAML 中为 UserControl 指定的名称将被使用它的地方(即在您的页面中)指定的任何名称覆盖。
解决方法是使用类似以下内容的内容:
其中 LayoutRoot 是 UserControl 的 XAML 内的根控件。
此外,您对 MaximumItems 属性的第一种方法是正确的。
I'm guessing that your TextBox is inside your UserControl. If so, then ElementName binding has an issue, as described here.
Basically, the name you give the UserControl in it's XAML is overwritten by any name given to it where it's used (i.e. in your Page).
The workaround is to use something like:
Where LayoutRoot is the root control inside the UserControl's XAML.
Also, your first approach to the MaximumItems property is correct.