所以這是我的第一個 MVVM 應用程式。我有一個名為 MainWindowViewModel 的“shell”視圖模型,用于主視窗,它基本上將視圖分成兩頁:MainWindowRibbon 和 MainWindowFrame。MainWindowViewModel 將兩個頁面都存盤為屬性,我計劃使用資料系結在 UI 中進行更新。以下是一些代碼供參考:
MainWindowView xaml~
<Grid>
<Frame Content="{Binding MainWindowRibbon}" Grid.Column="0" Grid.Row="0"/>
<ScrollViewer>
<Frame Content="{Binding MainWindowFrame}"/>
</ScrollViewer>
</Grid>
MainWindowView 代碼在后面~
public partial class MainWindowView : Window
{
public MainWindowView()
{
InitializeComponent();
mainWindowViewModel = new MainWindowViewModel();
DataContext = mainWindowViewModel;
}
public MainWindowViewModel mainWindowViewModel;
}
MainWindowViewModel代碼~
public MainWindowViewModel()
{
//MainWindowRibbon and MainWindowFrame are declared as public Page properties
MainWindowRibbon = new MainWindowRibbonView();
MainWindowFrame = new WelcomePageView();
}
MainWindowRibbonView 與 MainWindowView 一樣,實體化 MainWindowRibbonViewModel。
當我希望在 MainWindowRibbonViewModel 中使用將呼叫 MainWindowViewModel 重新分配 MainWindowFrame 頁面的事件時,我的麻煩就來了。我不知道如何連接我在 MainWindowRibbonView 中創建的導航欄的按鈕命令以在 MainWindowViewModel 中引發事件或更改。
I do not know if the way I have organized this is ideal. Please let me know if I need to revise.
If somebody could help me determine the best approach, or even just a functioning one, I would be very grateful.
P.S. Sorry if the naming conventions aren't the greatest.
Edit: Lesson learned: listen to Joe.
uj5u.com熱心網友回復:
我想這取決于您在導航欄中使用的按鈕型別。是RadioButton嗎?一個RibbonToggleButton?它是系結到的常規按鈕ICommand嗎?
由于您將導航欄稱為“功能區”,讓我們假設它是一個RibbonToggleButton(基本上仍然是一個復選框)。如果選中它,您將顯示一些視圖模型您的“第 1 頁”。如果未選中,您應該使用另一個視圖模型來代表您的“第 2 頁”
讓我們還假設您的視圖的功能區在頂部。所以你有兩行:功能區行和內容行。
我可能會將您的 MainWindow 重寫為如下所示,(請注意IsChecked視圖模型中某些布爾屬性的屬性,如下所示:)
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/> <!-- The "Ribbon" -->
<RowDefinition Height="*"/> <!-- The page content -->
</Grid.RowDefinitions>
<ToggleButton Content="Show Page 1" IsChecked="{Binding ShowPage1}"/>
<ScrollViewer Grid.Row=1>
<Frame Content="{Binding CurrentViewModel}"/>
</ScrollViewer>
</Grid>
我可能會這樣撰寫您的視圖模型:(請注意,我假設它實作了 INotifyPropertyChanged 并且我呼叫了一個RaisePropertyChanged未顯示的函式。
public class Page1ViewModel {} // Fill this out with Page 1 properties
public class Page2ViewModel {} // Fill this out with Page 2 properties
// MainWindowViewModel. Implements INotifyPropertyChanged. Implementation
// is not shown here.
public class MainWindowViewModel : INotifyPropertyChanged
{
private Page1ViewModel = new Page1ViewModel();
private Page2ViewModel = new Page2ViewModel();
public MainWindowViewModel()
{
_currentViewModel = Page1ViewModel;
ShowPage1 = true;
}
private object _currentViewModel;
// The current contents of the main frame.
public object CurrentViewModel
{
get => _currentViewModel;
set
{
if (value == _currentViewModel)
return;
_currentViewModel = value;
RaisePropertyChanged();
}
// Should CurrentViewModel be page 1 or page 2?
public bool ShowPage1
{
get => return _currentViewModel == Page1ViewModel;
set
{
if (value == ShowPage1)
return;
CurrentViewModel = value ? Page1ViewModel : Page2ViewModel;
RaisePropertyChanged();
}
}
}
請注意,我沒有向您展示 s 的任何屬性,Page1VieModel也沒有向您展示我假設您將為它們撰寫Page2ViewModel的隱含s。DataTemplate
此外,我假設您的導航欄(以及MainWindowView一般情況下)DataContext已設定為MainWindowViewModel
使用命令按鈕或 a 的實作RadioButton會完全不同。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/448337.html
