3

I have the following xaml -

<Window x:Class="DataTemplateTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="400" Width="600" Loaded="Window_Loaded">    
    <Grid>
        <ListBox Height="380" Margin="10,12,0,0" Width="355"/>
    </Grid>
</Window>  

and the following code-behind -

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        _nameList = new List<string>
                        {
                            "X",
                            "Y",
                            "Z"
                        };
    }

    private List<string> _nameList;
    public List<string> NameList
    {
        get { return _nameList; }
    }
}  

I want to set the NameList as the ItemsSource of the ListBox from the xaml, not from the code-behind. How do I do that?

EDIT : I know the MVVM-way of doing this. But that's not what I'm asking.

EDIT : It's not that I don't like MVVM or so. While doing some quick test I just realized that I don't know how to do this. So, wondering if it's possible, and trying to learn. Is it anyhow possible using StaticResource?

atiyar
  • 7,762
  • 6
  • 34
  • 75
  • 1
    Please explain why you refuse to use MVVM and also don't want to use code behind to set the list. You are already using code behind to create the list. Why not set it there? – Daniel Hilgarth Sep 10 '12 at 09:07
  • @DanielHilgarth: it's not refusal or anything like that. to perform a quick test i just got stuck here, and realized that i don't know how to do this. so i'm just trying to learn. – atiyar Sep 10 '12 at 09:13
  • Thanks. I was asking, because the motivation for using a different way than the one most would recommend has an impact on the answer I would give. – Daniel Hilgarth Sep 10 '12 at 09:16

1 Answers1

2

If you've meant on "not doing the MVVM-way" that you don't want to use ViewModels then you can data-bind to the "codebehind" with the following steps:

Set the binding in XAML:

<ListBox ItemSource="{Binding NameList}"/>

And set the DataContext to this after you have populated your list e.g in the Window_Loaded event:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    _nameList = new List<string>
                    {
                        "X",
                        "Y",
                        "Z"
                    };
    DataContext = this;
}

Edit: If you don't want to set the DataContext you can bind directly to the window:

<Window Name="window" ... />

  <ListBox  ItemsSource="{Binding NameList, ElementName=window}"/>

Or you can use AncestorBinding as

<ListBox ItemsSource="{Binding NameList, RelativeSource={RelativeSource AncestorType=Window}}"/>

However I both cases the list will be empty because the view won't be notified by the fact that you populated your list in the loaded event. So you need to use INPC to notify that the "NameList" property changed.

nemesv
  • 138,284
  • 16
  • 416
  • 359
  • can it be done without setting the `DataContext`? sorry, i forget to mention that in the question. – atiyar Sep 10 '12 at 09:20
  • For the DataBinding to work you need to set the `DataContext` somewhere. – nemesv Sep 10 '12 at 09:22
  • You don't have to set the DataContext in this case. You want to bind your ListBox to your windows property which is the ListBoxes ancestor. So just bind to its ancestor: http://stackoverflow.com/questions/84278/how-do-i-use-wpf-bindings-with-relativesource. – Miklós Balogh Sep 10 '12 at 09:30
  • @NerotheZero not with StaticResoure but with ElementName or with RelativeSource. See my updated answer. – nemesv Sep 10 '12 at 09:38