Windows Phone - 依赖属性

发布于 2024-11-02 10:42:17 字数 1179 浏览 0 评论 0原文

我正在尝试让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁微风不噪 2024-11-09 10:42:17

我猜您的 TextBox 位于您的 UserControl 内。如果是这样,则 ElementName 绑定存在问题,如所述 在这里

基本上,您在 XAML 中为 UserControl 指定的名称将被使用它的地方(即在您的页面中)指定的任何名称覆盖。

解决方法是使用类似以下内容的内容:

<TextBox Grid.Row="1" Grid.Column="1" x:Name="nudMaxIngredients" Width="120" Text="{Binding Parent.MaximumItems,Mode=TwoWay,ElementName=LayoutRoot}" InputScope="Number" />

其中 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:

<TextBox Grid.Row="1" Grid.Column="1" x:Name="nudMaxIngredients" Width="120" Text="{Binding Parent.MaximumItems,Mode=TwoWay,ElementName=LayoutRoot}" InputScope="Number" />

Where LayoutRoot is the root control inside the UserControl's XAML.

Also, your first approach to the MaximumItems property is correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文