Ok, so I've created two things, a MainWindowViewModel and a TabControlViewModel. Within my TabControlViewModel my View is basically a TabControl with 3 tabitems (Welcome/tabItem1/tabItem2).
My goal is when the application starts up I see the welcome tab only and then when I select File -> Open both tabItems become visible and the focus shows my tabItem2 displaying the text file content.
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";
}
I guess my biggest question is, should my Menu commands be in this TabControlViewModel or in my MainWindowViewModel? Many thanks for your patience here folks...:)