jay_t55
jay_t55

Reputation: 11662

Binding File names and contents with Metro ListBox

I am trying to bind data from text files (and the names of those files) to a ListBox in a C# Metro style app.

I've checked out the Metro Samples - I haven't been able to make too much sense out of them though. I'm relatively new to XAML to begin with, and throwing that ontop of Metro-style apps just adds to the confusion.

For something that is such a trivial task in WinForms, it feels like I'm trying to write my own OS!

Can someone please help me out a little? I've searched and searched but I just end up finding the same old tutorials/documentation; and maybe it's just me, but I feel that the documentation is lacking in this area. Maybe it's just not complete yet?

Any help at all will be much appreciated :)

Upvotes: 0

Views: 557

Answers (1)

Jordy van Eijk
Jordy van Eijk

Reputation: 2766

V declare a dictionary and loop through all your files adding the name as a key and the contents of the file as the value.

In XAML then bind the listbox to the items in the dictionary and set the displaymemberpath to the key of the KeyValuePair.

When selecting update the other control that will show your contents. I'll look if i can make a example for you in code.

Here the Example please do not look at the styling :) Used MVVM for this My ViewModel

namespace WPFTest.ViewModels
{
using System.IO;
using System.Windows.Input;

using Microsoft.Practices.Prism.Commands;

public class MainViewModel : NotificationObject
{
    public MainViewModel()
    {
        FileList = new Dictionary<string, string>();
        FillFileListCommand = new DelegateCommand(FillFileListExecuted);
    }

    private Dictionary<string, string> fileList;

    public Dictionary<string,string> FileList
    {
        get
        {
            return fileList;
        }
        set
        {
            fileList = value;
            RaisePropertyChanged(()=>FileList);
        }
    }

    public ICommand FillFileListCommand { get; set; }
    private void FillFileListExecuted()
    {
        var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        var files = Directory.GetFiles(path, "*.txt");
        var dict = new Dictionary<string, string>();
        foreach (var file in files)
        {
            using (var reader = new StreamReader(file))
            {
                var text = reader.ReadToEnd();
                dict.Add(file, text);
            }
        }
        FileList = dict;
    }
}

}

the xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="347*" />
        <RowDefinition Height="414*" />
    </Grid.RowDefinitions>
    <ListBox Height="701" Name="listBox1" Width="302" Margin="12,48,664,0" VerticalAlignment="Top" ItemsSource="{Binding FileList}" DisplayMemberPath="Key" Grid.RowSpan="2" />
    <TextBlock Height="737" HorizontalAlignment="Left" Margin="320,12,0,0" Name="textBlock1" Text="{Binding ElementName=listBox1, Path=SelectedItem.Value}" VerticalAlignment="Top" Width="646" TextWrapping="Wrap" Grid.RowSpan="2" />
    <Button Content="Fill" Height="30" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="302" Command="{Binding FillFileListCommand}" />
</Grid>

And in the codebehind of the xaml say

DataContext = new MainViewModel();

Upvotes: 1

Related Questions