将 DataTemplate 的数据上下文 属性 绑定到用户控件依赖项 属性
Binding DataTemplate's datacontext property to a usercontrol dependency property
在 UWP 应用程序中,我有一个带有数据模板的列表视图。 ListView 通过 ItemsSource 绑定到我的模型的 ObservableCollection。
<ListView SelectionMode="None"
ItemTemplate="{StaticResource MydataTemplate}"
ItemsSource="{Binding Meals, Mode=OneWay}"
Meals 是模型 Meal 的 ObservableCollection:
public class Meal
{
public string restaurantId { get; set; }
public double price { get; set; }
public int Quantity { get; set; } = 0;
}
在 ListView DataTemplate 内部,我有一个用户控件(ListView 的每个元素都有这个用户控件)。我想将当前用餐数量(数量属性)绑定到用户控件的依赖属性:
<DataTemplate x:Key="mydataTemplate">
...
<ccontrol:MyUC Value="{Binding Quantity, Mode=TwoWay}" Grid.Column="2" />
...
</DataTemplate>
使用用户控制 DP:
public sealed partial class MyUC : UserControl
{
public MyUC()
{
this.InitializeComponent();
this.DataContext = this;
}
public int Value
{
get { return (int)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
// Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int), typeof(ResaNumUpDown), new PropertyMetadata(0));
但是在运行时我有一个绑定表达式错误:
错误:BindingExpression 路径错误:在 'Project.MyUC'
上找不到 'Quantity'属性
它在 UserControl 上搜索,但我想在 DataTemplate 数据上下文中搜索数量 属性。
我红谁对我帮助不大
提前致谢,
此致
不要像在 MyUC
构造函数中那样显式设置 UserControl 的 DataContext
。这样做会阻止从 UserControl 的父控件继承 DataContext。
public MyUC()
{
this.InitializeComponent();
// this.DataContext = this; // remove this line
}
像
这样的绑定
<ccontrol:MyUC Value="{Binding Quantity}" />
期望当前(继承的)DataContext 中有一个 属性 Quantity
,这里是 ListView 的 ItemsSource
集合中的当前元素。当您明确设置 DataContext
.
时,这将不起作用
在 UserControl 的 XAML 中,编写一个绑定,通过指定绑定的 ElementName
属性:
以 UserControl 作为其源
<UserControl ... x:Name="self">
...
<TextBlock Text="{Binding Value, ElementName=self}" />
...
</UserControl>
在 UWP 应用程序中,我有一个带有数据模板的列表视图。 ListView 通过 ItemsSource 绑定到我的模型的 ObservableCollection。
<ListView SelectionMode="None"
ItemTemplate="{StaticResource MydataTemplate}"
ItemsSource="{Binding Meals, Mode=OneWay}"
Meals 是模型 Meal 的 ObservableCollection:
public class Meal
{
public string restaurantId { get; set; }
public double price { get; set; }
public int Quantity { get; set; } = 0;
}
在 ListView DataTemplate 内部,我有一个用户控件(ListView 的每个元素都有这个用户控件)。我想将当前用餐数量(数量属性)绑定到用户控件的依赖属性:
<DataTemplate x:Key="mydataTemplate">
...
<ccontrol:MyUC Value="{Binding Quantity, Mode=TwoWay}" Grid.Column="2" />
...
</DataTemplate>
使用用户控制 DP:
public sealed partial class MyUC : UserControl
{
public MyUC()
{
this.InitializeComponent();
this.DataContext = this;
}
public int Value
{
get { return (int)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
// Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int), typeof(ResaNumUpDown), new PropertyMetadata(0));
但是在运行时我有一个绑定表达式错误:
错误:BindingExpression 路径错误:在 'Project.MyUC'
上找不到 'Quantity'属性它在 UserControl 上搜索,但我想在 DataTemplate 数据上下文中搜索数量 属性。
我红
提前致谢,
此致
不要像在 MyUC
构造函数中那样显式设置 UserControl 的 DataContext
。这样做会阻止从 UserControl 的父控件继承 DataContext。
public MyUC()
{
this.InitializeComponent();
// this.DataContext = this; // remove this line
}
像
这样的绑定<ccontrol:MyUC Value="{Binding Quantity}" />
期望当前(继承的)DataContext 中有一个 属性 Quantity
,这里是 ListView 的 ItemsSource
集合中的当前元素。当您明确设置 DataContext
.
在 UserControl 的 XAML 中,编写一个绑定,通过指定绑定的 ElementName
属性:
<UserControl ... x:Name="self">
...
<TextBlock Text="{Binding Value, ElementName=self}" />
...
</UserControl>