c# - Windows Phone 10 Xaml ListView.ItemTemplate 绑定用户控件对象属性
问题描述
我正在尝试在 ListView.ItemTemplate 中使用自定义控件。此自定义控件具有对象属性。我无法绑定到此对象属性。我尝试了以下但抛出错误。
我的要求是,MyDataRowProperty 需要绑定到分配给 listView.ItemsSource 的 List 中的每个 MyDataRow。
<ListView x:Name="listView" ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<!--<Controls:DetailItemControl Height="105" Width="400" MyDataRowProperty="{Binding RelativeSource={RelativeSource Self}}"></Controls:DetailItemControl>-->
<Controls:DetailItemControl Height="105" Width="400" MyDataRowProperty="{Binding}"></Controls:DetailItemControl>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
后面的代码:
List<MyDataRow> rows = new List<MyDataRow>();
rows = GetData();
listView.ItemsSource = rows;
MyControl.xaml
<UserControl
x:Class="MyProject.Controls.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="110"
d:DesignWidth="400">
<Grid>
<TextBlock x:Name="tblDescription" />
</Grid>
</UserControl>
MyControl.xaml.cs:
private MyDataRow _MyDataRow;
public MyDataRowProperty MyDataRow
{
get { return _MyDataRow; }
set {
_MyDataRow = value;
if (_MyDataRow != null)
{
tblDescription.Text = _MyDataRow.Description
}
}
}
MyDataRow.cs
public class MyDataRow
{
public string Description { get; set; }
}
解决方案
有一些问题需要解决。首先,您应该从您的 ViewModel 中绑定项目,而不是从后面的代码中。在您的 ViewModel 中实现 INotifyPropertyChanged 接口。它应该如下所示:
public class YourViewModel: INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection <MyDataRow> rows;
public ObservableCollection<MyDataRow> Rows
{
get {return rows;}
set {
Rows = value;
NotifyPropertyChanged("Rows");
}
}
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
....
}
在您的组件中,您应该创建一个依赖属性:
public string MyDataRow
{
get { return (string)GetValue(MyDataRowProperty); }
set { SetValue(MyDataRowProperty, value); }
}
public static readonly DependencyProperty MyDataRowProperty = DependencyProperty.Register("MyDataRow", typeof(string), typeof(MyControl));
之后在你的View的代码后面设置datacontext,然后你可以在xaml中绑定它。
<ListView x:Name="listView" ItemsSource="{Binding Rows}">
<ListView.ItemTemplate>
<DataTemplate>
<Controls:DetailItemControl Height="105" Width="400" MyDataRow="{Binding Description}"></Controls:DetailItemControl>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
希望这可以帮助。随意问。我没有运行此代码,因此请检查语法,但想法就在那里。克里斯托夫
推荐阅读
- javascript - 在提交表单时禁用提交按钮,同时仍然允许 PHP 脚本运行
- java - 使用简单的json在java中解析JSON数组
- html - 如何从透明按钮中删除背景图像
- amazon-web-services - 我可以通过 Spring Boot 应用程序从 Redis 或某些 aws 服务读取外部配置吗
- java - 我怎样才能找到从一个瓷砖到另一个瓷砖必须传递的所有瓷砖
- php - 用长或复杂的形式在 PHP 中执行 POST 查询的最佳方法?
- python - 绘制很多点让我的电脑死机
- python - 如何在 python 电子邮件模块发送的表格中添加垂直滚动条?
- python - 改变神经网络中 Sigmoid 激活的阈值
- c++ - 成员不可访问 C++