Binding ListBox to List (Collection) in XAML

Posted by david2tm on Stack Overflow See other posts from Stack Overflow or by david2tm
Published on 2010-04-20T20:33:14Z Indexed on 2010/04/20 20:43 UTC
Read the original article Hit count: 431

Filed under:
|
|
|
|

Hello,

I'm learning WPF, so I'm kind of n00b in this. I saw some examples about how to do what I want to do, but nothing exactly...

The question: I want to bind List to ListBox. I want to do it in XAML, w/o coding in code behind. How can I achieve that?

Right now I do it that way:

<!-- XAML -->

<ListBox x:Name="FileList">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Label Content="{Binding Path=.}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

// Code behind
public MainWindow()
{
    // ...
    files = new List<string>();
    FileList.ItemsSource = files;
}

private void FolderBrowser_TextChanged(object sender, RoutedEventArgs e)
{
    string folder = FolderBrowser.Text;
    files.Clear();
    files.AddRange(Directory.GetFiles(folder, "*.txt", SearchOption.AllDirectories));
    FileList.Items.Refresh();
}

But I want to get rid of FileList.ItemsSource = files; and FileList.Items.Refresh(); in C# code.

Thanks

© Stack Overflow or respective owner

Related posts about wpf

Related posts about wpf-binding