[英]WPF C# MVVM ListView not updating
I see similar questions regarding ListView not updating in MVVM, however I have been struggling for quite a while already..我看到关于 ListView 没有在 MVVM 中更新的类似问题,但是我已经挣扎了很长一段时间了..
I have 2 classes, 1 of which is part of the other, such as:我有 2 个类,其中 1 个是另一个类的一部分,例如:
public class Info
{
public string Name { get; set; }
public string Status { get; set; }
....
public ObservableCollection<Content> UserContent { get; set; } = new ObservableCollection<Content>();
}
public class Content
{
public string Filename { get; set; }
public string Path { get; set; }
public string Type { get; set; }
}
The page is divided into 2 columns, left is itemscontrol and right is a single usercontrol.页面分为2列,左边是itemscontrol,右边是单个用户控件。 When an itemscontrol is clicked, it passes the datacontext to the usercontrol
单击项目控件时,它将数据上下文传递给用户控件
<local:ScreenControl DataContext="{Binding MainPageViewModel.SelectedDevice,
Source={x:Static local:ViewModelLocator.Instance}}" />
UserControl contains a TabControl. UserControl 包含一个 TabControl。 One of the tab display details from the Info class
Info class 中的选项卡显示详细信息之一
.....
<TextBlock Style="{StaticResource localTextBlock}" Text="{Binding Name}" />
<TextBlock Style="{StaticResource localTextBlock}" Text="{Binding Location}" />
.....
And up to here, all is good.到这里为止,一切都很好。
Problem starts on the other tab.问题从另一个选项卡开始。 I have a button that will OpenFileDialog, browse to video file and add the file to the UserContent.
我有一个按钮,它将打开文件对话框,浏览到视频文件并将文件添加到用户内容。 Then the listview should display.
然后应该显示列表视图。
public ICommand AddVidCommand { get; set; }
AddVidCommand = new RelayCommand(AddVid);
public void AddVid()
{
if (MainPageViewModel.SelectedDevice is ScreenInfo info)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Video Files|*.mp4;*.mkv;*.wmv";
if (openFileDialog.ShowDialog() == true)
{
info.UserContent.Add(new Content
{
Filename = openFileDialog.SafeFileName,
Path = openFileDialog.FileName
});
}
}
}
<TabItem Header="Playlist">
<StackPanel Orientation="Vertical" >
<Button Content="Add"
Command="{Binding ScreenControlViewModel.AddVidCommand,
Source={x:Static local:ViewModelLocator.Instance}}"
CommandParameter="{Binding}"/>
<ListView x:Name="fileList"
ItemsSource="{Binding UserContent}">
<ListView.View>
<GridView>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding Type}" Header="Type" />
<GridViewColumn Width="100" DisplayMemberBinding="{Binding Filename}" Header="Name" />
<GridViewColumn Width="100" DisplayMemberBinding="{Binding Path}" Header="Path" />
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</TabItem>
Adding MainPageViewModel添加 MainPageViewModel
public class MainPageViewModel : BaseViewModel
{
public static ObservableCollection<ScreenInfo> Devices { get; set; }
public static ScreenInfo SelectedDevice { get; set; }
public MainPageViewModel()
{
Devices = new ObservableCollection<ScreenInfo>();
}
}
As you probably guessed it, my listview does not update.您可能已经猜到了,我的列表视图没有更新。 I can clearly see the info being passed to the class in visual studio via the button command, but the UI doesn't show..
我可以清楚地看到通过按钮命令传递给 Visual Studio 中 class 的信息,但 UI 没有显示..
Answer provided by Clemens in a comment on the question: Clemens 在对该问题的评论中提供的答案:
You are mixing static and non-static members in your classes.您在类中混合 static 和非静态成员。 Each time an instance of MainPageViewModel is created, the static Devices property value is replaced with a new ObservableCollection, without notifying consumers of this property.
每次创建 MainPageViewModel 实例时,static Devices 属性值都会被新的 ObservableCollection 替换,而不会通知使用者该属性。 Don't use static properties with MVVM.
不要将 static 属性与 MVVM 一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.