Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.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# Listview绑定不会更新编程更改_C#_Wpf_Mvvm - Fatal编程技术网

C# Listview绑定不会更新编程更改

C# Listview绑定不会更新编程更改,c#,wpf,mvvm,C#,Wpf,Mvvm,我对这个话题做了一些研究,虽然我遇到了一些可能性,但对我来说没有任何效果 详细信息: 我正在使用MVVM设计模式开发WPF应用程序。在ViewModel中,我有一个列表的注释,这个类有一些属性(其中,注释)。我在VM上创建了一个属性,SelectedNote,用于保存当前选定的便笺 在我的视图中,我将列表视图控件绑定到列表QcNotes。我已将文本框绑定到SelectedNote属性。当我对文本框进行更改时,它们会正确地反映在列表视图的相应行中 问题: 我包含了一个RevertChanges命令

我对这个话题做了一些研究,虽然我遇到了一些可能性,但对我来说没有任何效果

详细信息:

我正在使用MVVM设计模式开发WPF应用程序。在ViewModel中,我有一个
列表
注释
,这个类有一些属性(其中,
注释
)。我在
VM
上创建了一个属性,
SelectedNote
,用于保存当前选定的便笺

在我的
视图中
,我将
列表视图
控件绑定到列表
QcNotes
。我已将
文本框
绑定到
SelectedNote
属性。当我对
文本框进行更改时,它们会正确地反映在
列表视图的相应行中

问题:

我包含了一个
RevertChanges
命令。这是一个相对简单的命令,用于撤消我对便笺所做的更改。它正确地更新了
文本框
,实际上也正确地更新了基础列表,但更改不会更新
列表视图
本身。(在这种情况下,是否有必要使用
可观察的集合
?我被要求尝试在不这样做的情况下解决问题)

尝试的修复

我试图在调用
RevertChanges
的过程中直接调用
NotifyPropertyChanged(“SelectedNote”)
NotifyPropertyChanged(“QcNotes”)
,但这并没有解决问题

有什么想法吗

XAML

<Window.DataContext>
    <VM:MainProjectViewModel />
</Window.DataContext>
<Grid>
    <StackPanel>
        <ListView ItemsSource="{Binding QcNotes, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" x:Name="list" SelectedItem="{Binding SelectedNote}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Note" DisplayMemberBinding="{Binding Note}" />
                </GridView>
            </ListView.View>
        </ListView>
        <TextBox
          Height="30"
          HorizontalAlignment="Stretch"
          Text="{Binding SelectedNote.Note, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
        />
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <Button Content="Allow Edits" Command="{Binding ChangeStateToAllowEditsCommand}" />
        <Button Content="Save Changes" Command="{Binding EditNoteCommand}" />
        <Button Content="Revert Changes" Command="{Binding RevertChangesToNoteCommand}" />
        </StackPanel>
    </StackPanel>
</Grid>

查看模型代码

public class MainViewModel : BaseViewModel
  { 
        private QcNote selectedNote;
        private string oldNoteForUpdating;
        private VMState currentState;
        private string noteInput;
        private IList<QcNote> qcNotes;

        public IList<QcNote> QcNotes
        {
            get
            {
                return qcNotes;
            }
            set
            {
                qcNotes = value;
                NotifyPropertChanged();
            }
        }

        public QcNote SelectedNote
        {
            get
            {
                return selectedNote;
            }
            set
            {
                selectedNote = value;
                oldNoteForUpdating = SelectedNote.Note;
                NotifyPropertChanged();
            }
        }

        public VMState CurrentState
        {
            get
            {
                return currentState;
            }
            set
            {
                currentState = value;
                NotifyPropertChanged();
            }
        }

        public ICommand RevertChangesToNoteCommand
        {
            get
            {
                return new ActionCommand(o => RevertChangestoNote());
            }
        }

        private void RevertChangestoNote()
        {
            QcNotes.First(q => q.Id == SelectedNote.Id).Note = oldNoteForUpdating;
            SelectedNote.Note = oldNoteForUpdating;
            NotifyPropertChanged("SelectedNote");
            NotifyPropertChanged("QcNotes");
            CurrentState = VMState.View;
        }
public类MainViewModel:BaseViewModel
{ 
私人QcNote selectedNote;
用于更新的专用字符串;
私人和国家;
私有字符串输入;
私人票据;
公共IList注释
{
得到
{
归还票据;
}
设置
{
qcNotes=价值;
NotifyPropertChanged();
}
}
公共注释已选择注释
{
得到
{
返回所选便笺;
}
设置
{
selectedNote=值;
oldnoteforupdate=已选择的Note.Note;
NotifyPropertChanged();
}
}
公共VMState当前状态
{
得到
{
返回电流状态;
}
设置
{
当前状态=值;
NotifyPropertChanged();
}
}
公共ICommand revertchangestonote命令
{
得到
{
返回新的ActionCommand(o=>RevertChangesNote());
}
}
私有无效RevertChangesNote()
{
QcNotes.First(q=>q.Id==SelectedNote.Id)。Note=oldnoteforupdate;
选择Note.Note=OldNote进行更新;
NotifyPropertChanged(“SelectedNote”);
NotifyPropertChanged(“QcNotes”);
CurrentState=VMState.View;
}

我会发布我自己问题的答案,但不想阻止其他人提供建议


我在我的
模型上实现了
INotifyPropertyChanged
接口。QcNote
类解决了这个问题。最初,该接口只在
ViewModel
上实现。在这种情况下,
NotifyPropertyChanged
仅在
QcNote
对象本身发生更改时调用,而不是在对象的属性已更改。

我将发布我自己问题的答案,但不想阻止其他人提供建议


我在我的
模型上实现了
INotifyPropertyChanged
接口。QcNote
类解决了这个问题。最初,该接口只在
ViewModel
上实现。在这种情况下,
NotifyPropertyChanged
仅在
QcNote
对象本身发生更改时调用,而不是在对象的属性已更改。

用于编辑/还原场景。此外,还原的前两行似乎执行相同的操作。
ObservableCollection
仅在修改集合本身(添加、删除、移动等)时才相关@H.B.感谢您的输入;我将查看
绑定组
RevertChanges
中的冗余是因为我尝试了不同类型的东西,以查看什么会粘住。我将删除它。不过,感谢您的提醒。这些都是为编辑/还原场景制作的。另外,还原的前两行似乎做了相同的事情。
ObservableCollection
仅在修改集合本身(添加、删除、移动等)时才相关@H.B.感谢您的输入;我将查看
BindingGroups
RevertChanges
中的冗余是因为我尝试了不同类型的东西,以查看什么会粘住。我将删除它。不过感谢您的提醒。