Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Viewmodel类型为Observable Collection的MVVM ListView未更新视图_C#_Xaml_Mvvm_View_Observable - Fatal编程技术网

C# Viewmodel类型为Observable Collection的MVVM ListView未更新视图

C# Viewmodel类型为Observable Collection的MVVM ListView未更新视图,c#,xaml,mvvm,view,observable,C#,Xaml,Mvvm,View,Observable,我的问题是,我在Viewmodel中添加了两个文件类型的对象(出于测试原因),这是一个observablecollection,但视图不会随着应用程序的启动而更新。 以下是Mainview.cs: public class MainView:ObservableCollection<Files> { public MainView() { Files x = new Files("picture", "jpg"); Files x1 =

我的问题是,我在Viewmodel中添加了两个文件类型的对象(出于测试原因),这是一个observablecollection,但视图不会随着应用程序的启动而更新。 以下是Mainview.cs:

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

  public class MainView:ObservableCollection<Files>
{
    public MainView()
    {
        Files x = new Files("picture", "jpg");
        Files x1 = new Files("soundfile", "mp3");
        Add(x);
        Add(x1);
    }
}
public类主视图:ObservableCollection
{
公共主视图()
{
文件x=新文件(“图片”、“jpg”);
文件x1=新文件(“声音文件”、“mp3”);
加(x);
添加(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>


您需要设置ListView的ItemsSource。在这种情况下,它与DataContext相同,因此只需执行以下操作:

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

您需要设置ListView的ItemsSource。在这种情况下,它与DataContext相同,因此只需执行以下操作:

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

像这样更改代码

你的“文件”类;这里没有什么变化。但是看看我实现属性和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);
        }
    }
}
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Windows.Input;
命名空间WpfApplication1
{
公共类MainWindowViewModel1:INotifyPropertyChanged
{
私有可观察收集文件;
私有文件\u选择的文件;
//私有ICommand_getFiles;
公共可观测收集文件
{
获取{return\u files;}
设置
{
_文件=值;
OnPropertyChanged(“文件”);
}
}
公共文件选择文件
{
获取{return\u selectedFile;}
设置
{
_selectedFile=value;
OnPropertyChanged(“SelectedFile”);
}
}
//公共ICommand获取文件
//{
//获取{return\u getFiles;}
//设置
//    {
//_getFiles=值;
//    }
//}
//公共void ChangeFileName(对象obj)
//{
//文件[0].FileName=“File_”+new Random().Next().ToString(CultureInfo.InvariantCulture);
//}
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
公共主窗口ViewModel1()
{
Files=新的observeCollection();
添加(新文件(){FileName=“picture”,DataType=“jpg”});
添加(新文件(){FileName=“soundfile”,DataType=“mp3”});
//GetFiles=newrelaycommand(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>


像这样更改代码

你的“文件”类;这里没有什么变化。但是看看我实现属性和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);
        }
    }
}
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Windows.Input;
命名空间WpfApplication1
{
公共类MainWindowViewModel1:INotifyPropertyChanged
{
私有可观察收集文件;
私有文件\u选择的文件;
//私有ICommand_getFiles;
公共可观测收集文件
{
获取{return\u files;}
设置
{
_文件=值;
OnPropertyChanged(“文件”);
}
}
公共文件选择文件
{
获取{return\u selectedFile;}
设置
{
_selectedFile=value;
OnPropertyChanged(“SelectedFile”);
}
}
//公共ICommand获取文件
//{
//获取{return\u getFiles;}
//设置
//    {
//_getFiles=值;
//    }
//}
//公共void ChangeFileName(对象obj)
//{
//文件[0].FileName=“File_”+new Random().Next().ToString(CultureInfo.InvariantCulture);
//}
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
公共主窗口ViewModel1()
{
Files=新的observeCollection();
添加(新文件(){FileName=“picture”,DataType=“jpg”});
添加(新文件(){FileName=“soundfile”,DataType=“mp3”});
//GetFiles=newrelaycommand(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>


这更像是一个理解问题。您的
文件
类应该是您的ViewModel<代码>文件
应该是ViewModel中的属性。@KosalaW您的意思是文件应该是t