簡體   English   中英

WPF C# MVVM 列表視圖未更新

[英]WPF C# MVVM ListView not updating

我看到關於 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,右邊是單個用戶控件。 單擊項目控件時,它將數據上下文傳遞給用戶控件

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


UserControl 包含一個 TabControl。 Info class 中的選項卡顯示詳細信息之一

.....
<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 中 class 的信息,但 UI 沒有顯示..

Clemens 在對該問題的評論中提供的答案:

您在類中混合 static 和非靜態成員。 每次創建 MainPageViewModel 實例時,static Devices 屬性值都會被新的 ObservableCollection 替換,而不會通知使用者該屬性。 不要將 static 屬性與 MVVM 一起使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM