好了,我已经创建了两个东西,一个MainWindowViewModel和TabControlViewModel。在我的TabControlViewModel内,我的视图基本上是一个带3个Tabitems的TabControl(Welcome/tabItem1/tabItem2)。WPF绑定用户控制
我的目标是当应用程序启动时,我只看到欢迎选项卡,然后当我选择文件 - >打开双方的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?您的耐心在这里的乡亲... :)
我上周读了这篇文章(不同的链接),并花了很长时间试图让它起作用。如果你可以发布一些代码,会非常感激。 – 2011-01-24 21:00:31