gpt4 book ai didi

silverlight - Windows Phone - 依赖属性

转载 作者:行者123 更新时间:2023-12-04 03:30:49 25 4
gpt4 key购买 nike

我正在尝试让 UserControl 在 Windows Phone 7 中工作。我有一些我想绑定(bind)的属性,但是无论我是否将它们添加为 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 元素称为“根”,但未填充该值。让它工作一半的唯一方法是使用这个:

public int MaximumItems
{
get { return Convert.ToInt32(DataContext) }
set { DataContext = value; }
}

似乎有什么东西干扰了 DataContext,但如果我绑定(bind)到 DependencyProperties 为什么会很重要?

最佳答案

我猜您的 TextBox 在您的 UserControl 中。如果是这样,则 ElementName 绑定(bind)有问题,如所述 here .

基本上,您在 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 属性的第一种方法是正确的。

关于silverlight - Windows Phone - 依赖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5719036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com