[英]Binding ObservableCollection to a listView programmatically
There are lot of question of how to bind an ObservableCollection to a ListView through XAML. 如何通过XAML将ObservableCollection绑定到ListView有很多问题。 I need though how to bind the collection to ListView programmatically. 我需要如何以编程方式将集合绑定到ListView。 There are some questions that help but they still do not address exactly what I need. 有一些问题可以帮助您,但仍然不能完全满足我的需求。
I have these classes: 我有这些课:
public class myTask
{
public DateTime TaskDate { get;}
public string TaskName { get;}
}
public class myTaskList : List<myTask>
{
public ObservableCollection<myTask> getMyTasks(DateTime d)
{
ObservableCollection<myTask> t = new ObservableCollection<myTask>
(this.Where(x => x.TaskDate == d).ToList<myTask>());
return t;
}
}
And I want to bind only the TaskName of the result of getMyTasks to a listview at runtime. 我只想在运行时将getMyTasks结果的TaskName绑定到列表视图。 I have tried this approach: 我已经尝试过这种方法:
ListView lv = new ListView();
//assuming I declare myTaskList a static class
lv.DataContext = myTaskList.getMyTasks(DateTime.Now);
var binding = new Binding();
binding.Source = lv;
lv.SetBinding(ListView.ItemsSourceProperty, binding);
That may only bind the results to the list, how do I only bind TaskName in the results? 那可能只将结果绑定到列表,我如何只在结果中绑定TaskName?
Without a good Minimal, Complete, and Verifiable example that clearly illustrates your question, it's impossible to know for sure what answer you need. 没有一个很好的最小,完整和可验证的示例清楚地说明您的问题,就不可能确定知道您需要什么答案。 But… 但…
There does not appear to be a binding operation here at all. 似乎根本没有绑定操作。 The binding code you show creates a circular binding (binding the ListView
object's ItemsSource
property to itself), which doesn't seem useful. 您显示的绑定代码创建一个循环绑定(将ListView
对象的ItemsSource
属性绑定到自身),这似乎没有用。 More to the point, you should be able to accomplish what you want simply by assigning the ItemsSource
property, and the DisplayMemberPath
property: 更重要的是,只需分配ItemsSource
属性和DisplayMemberPath
属性,您应该就能完成所需的工作:
lv.ItemsSource = myTaskList.getMyTasks(DateTime.Now);
lv.DisplayMemberPath = "TaskName";
No need to create a binding at all. 完全不需要创建绑定。
I think I've finally found a solution that is applicable to my scenario. 我想我终于找到了适用于我的方案的解决方案。
It is possible to bind a field of an ObservableCollection data set of multiple fields to a listview. 可以将多个字段的ObservableCollection数据集的字段绑定到列表视图。 What we need to do is declaring a data template in the resource: 我们需要做的是在资源中声明一个数据模板:
<DataTemplate x:Key='myTaskItemTemplate'>
<TextBlock x:Name='ItemTaskName'
FontSize='15'
HorizontalAlignment='Stretch'
VerticalAlignment='Stretch'
TextAlignment='Center'
Text='{Binding TaskName}' />
</DataTemplate>
Then create the listview using the above template 然后使用上面的模板创建listview
ListView lv = new ListView { ItemTemplate = (DataTemplate)Resources["myTaskItemTemplate"] };
and then create a binding with the source being the ObservableCollection Dataset bound to the ItemSourceProperty of the listview 然后创建一个绑定,其源是绑定到listview的ItemSourceProperty的ObservableCollection数据集
var t = myCalendarMonth.TaskList(d.theDate);
lv.DataContext = t;
var binding = new Binding();
binding.Source = t;
lv.SetBinding(ListView.ItemsSourceProperty, binding);
I am sure there may be a lot better solutions out there as I am very new to XAML but the approach discussed about seems to yield the results I am looking for. 我肯定会有更好的解决方案,因为我对XAML还是很陌生,但是讨论的方法似乎可以产生我想要的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.