Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set xaml code ItemsSource="{Binding}" with code behind

I have the following property Temp2: (my UserControl implements INotifyPropertyChanged)

    ObservableCollection<Person> _Temp2;
    public ObservableCollection<Person> Temp2
    {
        get
        { 
            return _Temp2; 
        }
        set
        {
            _Temp2 = value;
            OnPropertyChanged("Temp2");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

I need to create a listview dynamically. I have the following listview in XAML:

<ListView 
     Name="listView1" 
     DataContext="{Binding Temp2}" 
     ItemsSource="{Binding}" 
     IsSynchronizedWithCurrentItem="True">
 <ListView.View>
 .... etc

Now I am trying to create the same listview with c# as:

        ListView listView1 = new ListView();
        listView1.DataContext = Temp2;
        listView1.ItemsSource = Temp2; // new Binding(); // ????? how do I have to implement this line
        listView1.IsSynchronizedWithCurrentItem = true;
        //.. etc

when I populate the listview with C# the listview does not get populated. what am I doing wrong?

like image 317
Tono Nam Avatar asked Mar 13 '12 21:03

Tono Nam


People also ask

How do you bind in XAML?

Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.

What is code behind in WPF?

Code-behind is a term used to describe the code that is joined with markup-defined objects, when a XAML page is markup-compiled. This topic describes requirements for code-behind as well as an alternative inline code mechanism for code in XAML.

What is ItemsSource in XAML?

ItemsSource can be data bound to any sequence that implements the IEnumerable interface, although the type of collection used does determine the way in which the control is updated when items are added to or removed. When ItemsSource is set, the Items property cannot be used to control the displayed values.

What does binding DO WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.


1 Answers

You need to create a Binding object.

Binding b = new Binding( "Temp2" ) {
    Source = this
};
listView1.SetBinding( ListView.ItemsSourceProperty, b );

The argument passed to the constructor is the Path that you're used to from XAML bindings.

You can leave out the Path and Source if you set the DataContext to Temp2 as you do above, but I personally think it's preferable to bind to a ViewModel (or other data source) and use a Path than to directly bind to a class member.

like image 66
kitti Avatar answered Sep 30 '22 18:09

kitti