【发布时间】:2011-01-24 20:17:25
【问题描述】:
好的,所以我创建了两个东西,一个 MainWindowViewModel 和一个 TabControlViewModel。在我的 TabControlViewModel 中,我的 View 基本上是一个带有 3 个 tabitems 的 TabControl(Welcome/tabItem1/tabItem2)。
我的目标是当应用程序启动时,我只看到欢迎选项卡,然后当我选择 File -> Open both tabItems 变得可见并且焦点显示我的 tabItem2 显示文本文件内容。
MainWindow.Xaml
<Menu DockPanel.Dock="Top" Width="Auto" Height="25" Name="Menu1">
<MenuItem Header="_File" VerticalContentAlignment="Top" >
<MenuItem Header="_New" Command="{Binding NewCommand}" />
<MenuItem Header="_Open" Command="{Binding OpenCommand}">
TabControlViewModel.cs
class TabControlViewModel : TabContainer
{
private DelegateCommand openCommand;
public ICommand OpenCommand
{
get
{
if (openCommand == null)
openCommand = new DelegateCommand(Open);
return openCommand;
}
}
private void Open(object obj)
{
ProcessOpenCommand();
}
private void ProcessOpenCommand()
{
if (dataChanged)
{
SaveFirst();
ShowOpenDialog();
}
else
{
ShowOpenDialog();
}
}
private void ShowOpenDialog()
{
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
ofd.Filter = "Text File (*.txt)|*.txt";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
filePath = ofd.FileName;
ReadFile(filePath);
SetTitle(ofd.SafeFileName);
RuleTab.Focus();
}
}
private string SaveFirst()
{
MessageBoxResult mbr = System.Windows.MessageBox.Show("Do you want to save changes?", "Save Changes", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning);
if (mbr == MessageBoxResult.Yes)
{
if (filePath != null)
{
SaveFile(filePath);
}
else
{
ProcessSaveCommand();
}
}
else if (mbr == MessageBoxResult.Cancel)
{
return "Cancel";
}
return "Nothing";
}
我想我最大的问题是,我的菜单命令应该在这个 TabControlViewModel 中还是在我的 MainWindowViewModel 中?非常感谢大家的耐心等待......:)
【问题讨论】:
标签: c# wpf binding tabcontrol