2016-01-11 84 views
1

我的問題是,我添加了2個對象的類型文件(出於測試的原因)在Viewmodel這是一個observablecollection,但視圖不會更新與啓動的應用。 這裏是Mainview.cs:MVVM ListView與Viewmodel的Observable Collection類型不更新視圖

public class MainView:ObservableCollection<Files> 
{ 
    public MainView() 
    { 
     Files x = new Files("picture", "jpg"); 
     Files x1 = new Files("soundfile", "mp3"); 
     Add(x); 
     Add(x1); 
    }} 

我做了什麼錯?以及如何避免這個錯誤?如果我不更改Files類中的屬性,並且只想在創建新的File對象時更新視圖,那麼是否真的需要INotifyPropertyChanged?

我得到了這個類:

public class Files:INotifyPropertyChanged 
{ 
    private String _fileName; 

    public String FileName 
    { 
     get { return _fileName; } 
     set { _fileName = value; OnPropertyChanged("FileName"); } 
    } 
    private String _dataType; 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    public String DataType 
    { 
     get { return _dataType; } 
     set { _dataType = value; OnPropertyChanged("DataType"); } 
    } 
    public Files(string filename, string dataType) 
    { 
     this._fileName = filename; 
     this._dataType = dataType; 
    } 
} 

而這個視圖模型:

public class MainView:ObservableCollection<Files> 
{ 
    public MainView() 
    { 
     Files x = new Files("picture", "jpg"); 
     Files x1 = new Files("soundfile", "mp3"); 
     Add(x); 
     Add(x1); 
    } 
} 

而且在XAML這樣做:

<Window x:Class="ClientTestDesign.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:ClientTestDesign" 
    xmlns:vm="clr-namespace:ClientTestDesign.ViewModel" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525" 
    > 
<Window.Resources> 
    <vm:MainView x:Key="View"></vm:MainView> 
</Window.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="75"></RowDefinition> 
     <RowDefinition Height="*"></RowDefinition> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition></ColumnDefinition> 
     <ColumnDefinition></ColumnDefinition> 
     <ColumnDefinition></ColumnDefinition> 
    </Grid.ColumnDefinitions> 
    <Button Grid.Column="0" Style="{DynamicResource ForwardBackButton}" Content="Back"></Button> 
    <Button Grid.Column="1" Style="{DynamicResource StopButton}" Content="Pause"></Button> 
    <Button Grid.Column="2" Style="{DynamicResource ForwardBackButton}" Content="Forward"></Button> 
    <ListView Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.ColumnSpan="3"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <TextBlock Text="{Binding /FileName, Source={StaticResource View}}"></TextBlock> 
        <TextBlock Text="{Binding /FileName, Source={StaticResource View}}"></TextBlock> 
       </StackPanel> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
</Grid> 

+0

這更是一個認識問題。你的'Files'類應該是你的ViewModel。 'Files'應該是ViewModel中的一個屬性。 –

+0

@KosalaW你的意思是文件應該是可觀察的集合?我的fviewmodel應該包含文件的屬性? –

+0

@KosalaW這不可能是唯一的錯誤,可以嗎?我改變它沒有工作 –

回答

2

更改您這樣的代碼;

您的「文件」類;這裏沒有太多變化。但看看我已經實現了屬性和OnPropertyChanged的方式。

using System.ComponentModel; 

namespace WpfApplication1 
{ 
public class File:INotifyPropertyChanged 
{ 
    private string _fileName; 
    private string _dataType; 

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

    public string DataType 
    { 
     get { return _dataType; } 
     set 
     { 
      _dataType = value; 
      OnPropertyChanged("DataType"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
} 

這是你的ViewModel。正如你所看到的,這個視圖模型與你的視圖模型完全不同。這裏有一些註釋的屬性/方法。如果您打算實施一些命令,那麼該代碼可能很有用。

using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Windows.Input; 

namespace WpfApplication1 
{ 
    public class MainWindowViewModel1:INotifyPropertyChanged 
    { 
     private ObservableCollection<File> _files; 
     private File _selectedFile; 
     //private ICommand _getFiles; 

     public ObservableCollection<File> Files 
     { 
      get { return _files; } 
      set 
      { 
       _files = value; 
       OnPropertyChanged("Files"); 
      } 
     } 

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

     //public ICommand GetFiles 
     //{ 
     // get { return _getFiles; } 
     // set 
     // { 
     //  _getFiles = value; 
     // } 
     //} 

     //public void ChangeFileName(object obj) 
     //{ 
     // Files[0].FileName = "File_" + new Random().Next().ToString(CultureInfo.InvariantCulture); 
     //} 

     public event PropertyChangedEventHandler PropertyChanged; 
     private void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     public MainWindowViewModel1() 
     { 
      Files = new ObservableCollection<File>(); 
      Files.Add(new File() { FileName = "picture", DataType = "jpg"}); 
      Files.Add(new File() { FileName = "soundfile", DataType = "mp3" }); 
      //GetFiles = new RelayCommand(ChangeFileName, param => true); 
     } 
    } 
} 

您的看法;看看綁定。這是你的問題之一。

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" 
    xmlns:vm="clr-namespace:WpfApplication1"> 
<Window.Resources> 
    <vm:MainWindowViewModel1 x:Key="View"></vm:MainWindowViewModel1> 
</Window.Resources> 
<Grid> 
    <ListView HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Source={StaticResource View}, Path=Files, Mode=TwoWay}" SelectedItem="{Binding Source={StaticResource View}, Path= SelectedFile, Mode=TwoWay}" Margin="28,27,31,146"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <TextBlock Text="{Binding Path= FileName, Mode=TwoWay}"></TextBlock> 
        <TextBlock Text="{Binding Path= DataType, Mode=TwoWay}"></TextBlock> 
       </StackPanel> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
    <!--<Button Content="Refresh" Command="{Binding Source={StaticResource View},Path = GetFiles}" HorizontalAlignment="Left" Height="33" Margin="347,236,0,0" VerticalAlignment="Top" Width="139"/>--> 
</Grid> 

+0

我給了一個嘗試 –

+0

它的工作,我用你的代碼,並刪除了我所有的額外網格和列+ rowdefinitions然後它的工作。謝謝你,hava一個可觀察的集合作爲一個對象在列表視圖中,我很高興它的工作:D –

1

您需要設置ListView的的ItemsSource。在這種情況下,它與您的DataContext的所以才這樣做:

<ListView ItemsSource="{Binding}" ... etc... 
+0

沒有工作,是不是從DataContext的ItemsSource繼承,如果它沒有設置明確 –

+0

不,它是完全獨立於DataContext。它對我使用你發佈的代碼很有效,所以你必須做一些你沒有向我們展示過的東西。 –

+0

那麼有2個可見的listobjects?我沒有改變任何不起作用的東西:( –