亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

WPF ObservableCollection 未在功能區視圖中更新

WPF ObservableCollection 未在功能區視圖中更新

C#
達令說 2023-07-09 09:55:19
我創建了一個 C# WPF 應用程序,其中的 RibbonApplicationMenu 顯示最近使用的 (MRU) 列表。不幸的是,當我從列表中選擇現有文件或上傳新文件時,顯示不會更新。在 XAML 中我有:<local:MostRecentFiles x:Key="MostRecentFilesData" />    ...<ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>    <ribbon:RibbonGallery Name="RecentDocuments" CanUserFilter="False"         SelectedValue="{Binding MostRecentFile, UpdateSourceTrigger=PropertyChanged}">        <ribbon:RibbonGalleryCategory Header="Recent Documents"            ItemsSource="{DynamicResource MostRecentFilesData}">        </ribbon:RibbonGalleryCategory>    </ribbon:RibbonGallery></ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>DataContext 設置為包含以下內容的類private ObservableCollection<string> _mostRecentFile = new ObservableCollection<string>();public ObservableCollection<string> MostRecentFile{    get { return _mostRecentFile; }    set    {        _mostRecentFile = value;        OnPropertyChanged("MostRecentFile");    }}public event PropertyChangedEventHandler PropertyChanged;protected virtual void OnPropertyChanged(string propertyName){    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}在 OpenFile 例程中,代碼是MostRecentFiles mrf = new MostRecentFiles();mrf.AddMRUitem(openFileDlg.FileName);MostRecentFiles 類包含主要的類方法,我在代碼中放置了一些示例文件路徑。public class MostRecentFiles : ObservableCollection<string>{    public ObservableCollection<string> MRUmenuItems = new ObservableCollection<string>();    public MostRecentFiles()    {        AddMRUitem(@"C:\MyDocuments\File3.txt"); //        AddMRUitem(@"C:\MyDocuments\File2.txt"); // } Sample files        AddMRUitem(@"C:\MyDocuments\File1.txt"); //    }在UpdateMRUList()中取消刪除OnPropertyChanged會產生錯誤:錯誤 CS1503 參數 1:無法從 'string' 轉換為 'System.ComponentModel.PropertyChangedEventArgs'當我啟動程序時,菜單正確顯示三個文件,但當我選擇一個文件時,顯示的順序不會改變;我希望所選文件移至列表頂部。同樣,當我打開新文件時,文件名不會添加到 MRU 中。但是,如果我單步執行代碼,列表就會按正確的順序更新。我做錯了什么?
查看完整描述

1 回答

?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

您正在綁定SelectedValue到一個集合。您不需要自定義集合。只需添加ObservableCollection到您的視圖模型并移動所選項目上的項目已更改:


查看型號:


private void OnSelectedMostRecentFileChanged()

{

  // Move the selected item to the front of the list

  this.MostRecentFiles.Move(this.MostRecentFiles.IndexOf(this.SelectedRecentFile), 0);

}


private string _selectedRecentFile;

public string SelectedRecentFile

{

    get { return _selectedRecentFile; }

    set

    {

        _selectedRecentFile= value;

        OnSelectedMostRecentFileChanged();

        OnPropertyChanged(nameof(SelectedRecentFile));

    }

}


private ObservableCollection<string> _mostRecentFiles = new ObservableCollection<string>();

public ObservableCollection<string> MostRecentFiles

{

    get { return _mostRecentFiles; }

    set

    {

        _mostRecentFiles = value;

        OnPropertyChanged(nameof(MostRecentFiles));

    }

}

看法:


<ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>

    <ribbon:RibbonGallery Name="RecentDocuments" CanUserFilter="False" 

        SelectedItem="{Binding SelectedRecentFile}">

        <ribbon:RibbonGalleryCategory Header="Recent Documents"

            ItemsSource="{Binding MostRecentFiles}">

        </ribbon:RibbonGalleryCategory>

    </ribbon:RibbonGallery>

</ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>


查看完整回答
反對 回復 2023-07-09
  • 1 回答
  • 0 關注
  • 195 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號