0

In code I can do "listView.ItemsSource = testlist" but how do I do the same in XAML?

My Window class has:

List<string> testlist = new List<string>();

In the constructor I have:

listView.ItemsSource = testlist;

The XAML is:

<ListView x:Name="listView" Margin="0" />

If I add to testlist then I get the list, but I just cannot figure out what the syntax is to assign the ItemsSource in XAML. In other words, I want to know how to assign the ItemsSource in XAML instead of in code.

It is my impression that XAML must make everything complicated.

I am sure there is an answer already, but I can't find any. I have looked at many questions and articles. All the questions and articles are about more complicated requirements or are only partial and don't show all the pieces even when the sample is as small as this.

What I really want to do is to create a template for the ListView with a TextBox in it but I want to at least be able to do it without even that.

Clarification: I am trying to do all the binding in code and with an existing class such as List without creating an additional class just to hold an array of strings, if that is possible. I want to know what the simplest solution would be.

The following is the complete MainWindow code:

public partial class MainWindow : Window
{
    List<string> testlist = new List<string>();

    public MainWindow()
    {
        InitializeComponent();
        testlist.Add("One");
        testlist.Add("Two");
        testlist.Add("Three");
    }
}

The following is the XAML except the Window tag:

<Grid>
    <ListView x:Name="listView" Margin="0" />
</Grid>
Sam Hobbs
  • 2,594
  • 3
  • 21
  • 32
  • 1
    You need to write a viewmodel, make your list a property of the viewmodel, and then bind the list to ItemsSource. Here's a simple example: http://www.c-sharpcorner.com/UploadFile/mahakgupta/simple-data-binding-in-wpf/ Don't take your impressions of WPF too seriously, since as you say you don't know anything about it yet. Net, it simplifies things. – 15ee8f99-57ff-4f92-890c-b56153 Jun 13 '16 at 03:22
  • I am sorry, I don't want to add complexity, I don't want to use viewmodel. – Sam Hobbs Jun 13 '16 at 04:03
  • @Ed Plunkett, for what it is worth, I edited that article Simple Data Binding in WPF. As for "you say you don't know anything about it", I did not say that. I won't comment further since this is not a place for chit-chat. – Sam Hobbs Jun 13 '16 at 06:31
  • You're on SO asking for help with something incredibly easy to do right. It's your own time you're wasting, not mine. Have fun. – 15ee8f99-57ff-4f92-890c-b56153 Jun 13 '16 at 12:00

3 Answers3

4

You can bind to the property in code-behind by specifying the RelativeSource on the binding. check this.

RelativeSource={RelativeSource Self}

In your case, you can create a property testlist in the code behind and the following xaml.

Code-behind

public ObservableCollection<string> testlist { get; set; } = new ObservableCollection<string>();

XAML

<Window ...
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
      <Grid >
         <ListView ItemsSource="{Binding testlist}"/>
      </Grid>
</Window>
Community
  • 1
  • 1
KDR
  • 478
  • 6
  • 19
  • Thank you, that is the type of answer I am hoping for, but it does not work for some reason. I checked and during execution the ItemsSource is null. I could provide more of my code but there is **very little** more. – Sam Hobbs Jun 13 '16 at 04:15
  • Yes, that works. It is a bit more complicated than I was hoping for but it is close enough. I will play with it to see if I can simplify it. For example, the "public" is a likely requirement but others might not be. I think WPF requires a property (as in "{ get; set; }" so it is interesting if that is not built into any existing class. The only simplification is that List can be used instead of ObservableCollection if we want to do that. – Sam Hobbs Jun 13 '16 at 04:58
1

You can use MVVM. Simple example here.

In this example I bind ListBox with TextBlock and Button to ViewModel. And then I add couple items to ListBox through code.

FriendsKenny
  • 150
  • 3
  • 11
1

I just cannot figure out what the syntax is to assign the ItemsSource in XAML.

You could set ItemsSource in Xaml file as defined.

<ListView x:Name="listView" Margin="0" ItemsSource="{Binding}">

and in Xaml.cs file, set the DataContext.

public MainWindow()
{
    InitializeComponent();      
    this.DataContext = list; // set your DataContext
}

Things may work doing above, but I would suggest explore MVVM (A pattern for modern UI development platforms) and do it that way, as you mentioned (in the question) you could find plenty of examples online.

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • Well I am sorry but that still requires a line of code so it does not help. – Sam Hobbs Jun 13 '16 at 04:05
  • So you want to avoid setting `DataContext` in the cs file and do it in Xaml? – Hari Prasad Jun 13 '16 at 04:06
  • I am saying that whether I set ItemsSource in code or DataContext in code, we are still doing something in code. It is no big problem but I want to know how to do it all in XAML **if possible**. – Sam Hobbs Jun 13 '16 at 04:33