简体   繁体   中英

Binding ObservableCollection to a listView programmatically

There are lot of question of how to bind an ObservableCollection to a ListView through XAML. I need though how to bind the collection to ListView programmatically. 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. 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?

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. More to the point, you should be able to accomplish what you want simply by assigning the ItemsSource property, and the DisplayMemberPath property:

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. 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 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

    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM