這是我制作標簽的第一堂課,我想將標簽添加到 ObservableCollection
namespace Notepad__.ViewModel
{
public class TabViewModel
{
public DocumentModel m_doc;
public ObservableCollection<TabItem> Tabs { get; set; }
public TabViewModel()
{
Tabs = new ObservableCollection<TabItem>();
Tabs.Add(new TabItem { Header = "s", Content = "One's content" });
Tabs.Add(new TabItem { Header = "Two", Content = "Two's content" });
}
public class TabItem
{
public string Header { get; set; }
public string Content { get; set; }
}
}
}
這是我想要訪問集合以添加新選項卡的類。創建新檔案時,我想將該檔案添加到集合和我的選項卡控制元件以更新選項卡項
namespace Notepad__.ViewModel
{
public class FileModel
{
public DocumentModel Document { get; private set; }
public ICommand NewCommand { get; }
public ICommand SaveCommand { get; }
public ICommand OpenCommand { get; }
public ICommand SaveAsCommand { get; }
public FileModel(DocumentModel document)
{
Document = document;
NewCommand = new RelayCommand(NewFile);
SaveCommand = new RelayCommand(SaveFile);
SaveAsCommand = new RelayCommand(SaveFileAs);
OpenCommand = new RelayCommand(OpenFile);
}
public void NewFile()
{
Document.FileName = "New File";
Document.FilePath = string.Empty;
Document.Text = string.Empty;
//TabViewModel.TabItem("")
}
uj5u.com熱心網友回復:
我如何從另一個類 C# 訪問我的 ObservableCollection
使集合成為靜態的。
public static ObservableCollection<TabItem> Tabs { get; set; }
然后像這樣訪問
TabViewModel.Tabs.Add(...);
請注意,這種情況下的 WPF 首選項是在 上創建視圖模型參考,app然后static,由主頁和其他人參考它。比如App.VM.Tabs.Add()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/443582.html
上一篇:如何使用反向方法創建回文
