【问题标题】:List box item Binding from ObservableCollection来自 ObservableCollection 的列表框项绑定
【发布时间】:2011-07-20 09:54:06
【问题描述】:

我有一个 ObservableCollection 文件作为

ObservableCollection>FileList> 文件列表; FileList 实体类有两个属性 FileName 和 FilePath。我想将文件名绑定到列表框中,并且在列表框中选择项目(文件名)时需要将文件的内容显示到文本块中。 我正在遵循 MVVM 模式。

我怎样才能实现这个..?

提前致谢..

【问题讨论】:

  • 我们可以假设..所有文件都是txt文件..?
  • 所有文件都是txtFiles...

标签: wpf binding


【解决方案1】:

我已经用 MVVM 创建了一个示例..

Xaml 代码:

<Grid Name="layoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <ListBox ItemsSource="{Binding FilesCollection}" SelectedItem="{Binding SelectedFile}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding FileName}"></TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <TextBlock Text="{Binding FileContent}" Grid.Column="1"></TextBlock>
</Grid>

Xaml 代码隐藏:

 InitializeComponent();
            this.DataContext = new ViewModel();

这是您的视图模型

public  class ViewModel : ViewModelBase
{
    public ObservableCollection<Fileinfo> FilesCollection { get; set; }
    private string _fileContent;

    public string FileContent
    {
        get { return _fileContent; }
        set
        {
            _fileContent = value;
            OnPropertyChanged("FileContent");
        }
    }
    private Fileinfo _selectedFile;

    public Fileinfo SelectedFile
    {
        get { return _selectedFile; }
        set
        {
            _selectedFile = value;
            OnPropertyChanged("SelectedFile");
            GetFileCotnent();
        }
    }

    private void GetFileCotnent()
    {
        using (StreamReader sr = new StreamReader(SelectedFile.FilePath))
        {
            FileContent = sr.ReadToEnd();
        }
    }

    public ViewModel()
    {
        FilesCollection = new ObservableCollection<Fileinfo>();
        string[] files = Directory.GetFiles(@"C:\files");
        foreach (var item in files)
        {
            FilesCollection.Add(new Fileinfo(item.Substring(item.LastIndexOf('\\')), item));
        }
    }

}

public class Fileinfo : ViewModelBase
{
    public Fileinfo(string filename, string filepath)
    {
        this.FileName = filename;
        this.FilePath = filepath;
    }
    private string _fileName;

    public string FileName
    {
        get
        {
            return _fileName;
        }
        set
        {
            _fileName = value; OnPropertyChanged("FileName");
        }
    }

    private string _filePath;

    public string FilePath
    {
        get { return _filePath; }
        set
        {
            _filePath = value;
            OnPropertyChanged("FilePath");
        }
    }

}

public   class ViewModelBase : INotifyPropertyChanged
{
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

【讨论】:

  • 现在我想扩展它以使用菜单显示来自不同文件夹的文件。当我单击第二个菜单项时使用相同的代码显示错误,因为所选项目为空。你可以请看看这个..
猜你喜欢
  • 2018-08-13
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
  • 2011-08-16
  • 2012-05-25
  • 2012-03-20
相关资源
最近更新 更多