【问题标题】:WPF C# MVVM ListView not updatingWPF C# MVVM ListView 未更新
【发布时间】:2020-08-09 01:45:08
【问题描述】:

我看到关于 ListView 未在 MVVM 中更新的类似问题,但是我已经苦苦挣扎了很长一段时间......
我有 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; }

    }


页面分为2列,左边是itemscontrol,右边是单个usercontrol。单击项目控件时,它将数据上下文传递给用户控件

<local:ScreenControl DataContext="{Binding MainPageViewModel.SelectedDevice, 
       Source={x:Static local:ViewModelLocator.Instance}}" />


UserControl 包含一个 TabControl。其中一个选项卡显示来自 Info 类的详细信息

.....
<TextBlock Style="{StaticResource localTextBlock}" Text="{Binding Name}" />
<TextBlock Style="{StaticResource localTextBlock}" Text="{Binding Location}" />
.....


到这里为止,一切都很好。

问题从另一个选项卡开始。我有一个按钮,它将打开文件对话框,浏览到视频文件并将文件添加到用户内容。然后应该显示列表视图。

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>

添加 MainPageViewModel

    public class MainPageViewModel : BaseViewModel
    {
        public static ObservableCollection<ScreenInfo> Devices { get; set; }

        public static ScreenInfo SelectedDevice { get; set; }

        public MainPageViewModel()
        {
            Devices = new ObservableCollection<ScreenInfo>();

        }
    }



您可能已经猜到了,我的列表视图没有更新。我可以清楚地看到通过按钮命令传递给 Visual Studio 中的类的信息,但 UI 没有显示..

【问题讨论】:

  • 确保从 AddVid 方法访问的 MainPageViewModel 实例与 UI 中使用的实例相同。这只是一个猜测,因为您没有向我们提供有关您的视图模型的足够信息。
  • 您在类中混合了静态和非静态成员。每次创建 MainPageViewModel 实例时,静态 Devices 属性值都会被新的 ObservableCollection 替换,而不会通知使用者该属性。不要在 MVVM 中使用静态属性。
  • 宾果游戏,谢谢克莱门特。这就是问题所在。请把它作为答案。

标签: c# wpf listview mvvm binding


【解决方案1】:

克莱门斯在对该问题的评论中提供的答案:

您在类中混合了静态和非静态成员。每次创建 MainPageViewModel 实例时,静态 Devices 属性值都会被新的 ObservableCollection 替换,而不会通知使用者该属性。不要在 MVVM 中使用静态属性。

【讨论】:

    猜你喜欢
    • 2014-09-24
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    相关资源
    最近更新 更多