Mini Kabibi Habibi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using DevExpress.Xpf.Bars;
using System.Collections.Specialized;
using DevExpress.Xpf.Ribbon;
using DevExpress.Xpf.Core;
using DevExpress.Utils;
namespace RibbonDemo {
public partial class MVVMRibbon : RibbonDemoModule {
private static ResourceDictionary sharedResources;
public static ResourceDictionary SharedResources {
get { return sharedResources; }
set { sharedResources = value; }
}
public MVVMRibbon() {
InitializeComponent();
SharedResources = Resources;
InitializeViewModel();
}
private void InitializeViewModel() {
ViewModel viewModel = new ViewModel();
FillViewModel(viewModel, textBox);
DataContext = viewModel;
}
protected override void OnUnloaded(object sender, RoutedEventArgs e) {
SharedResources = null;
base.OnUnloaded(sender, e);
}
private void FillViewModel(ViewModel viewModel, TextBox textBox) {
PageModel homePage = new PageModel() { Name = "Home" };
PageGroupModel clipboardGroup = new PageGroupModel() { Name = "Clipboard", Glyph = GlyphHelper.GetGlyph("/Images/Icons/paste-32x32.png") };
PageGroupModel addingGroup = new PageGroupModel() { Name = "Addition", Glyph = GlyphHelper.GetGlyph("/Images/Icons/Add_32x32.png") };
CategoryModel category = new CategoryModel();
viewModel.Categories.Add(category);
category.Pages.Add(homePage);
homePage.Groups.Add(clipboardGroup);
homePage.Groups.Add(addingGroup);
CommandModel cutCommand = new CommandModel(cutCommandExecuteFunc) { Caption = "Cut", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/cut-32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/cut-16x16.png") };
CommandModel copyCommand = new CommandModel(copyCommandExecuteFunc) { Caption = "Copy", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/copy-32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/copy-16x16.png") };
CommandModel pasteCommand = new CommandModel(pasteCommandExecuteFunc) { Caption = "Paste", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/paste-32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/paste-16x16.png") };
CommandModel selectCommand = new CommandModel(selectAllCommandExecuteFunc) { Caption = "Select All", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/SelectAll_32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/SelectAll_16x16.png") };
CommandModel blankCommand = new CommandModel(blankCommandExecuteFunc) { Caption = "Clear Page", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/new-32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/new-16x16.png") };
clipboardGroup.Commands.Add(cutCommand);
clipboardGroup.Commands.Add(copyCommand);
clipboardGroup.Commands.Add(pasteCommand);
clipboardGroup.Commands.Add(selectCommand);
clipboardGroup.Commands.Add(blankCommand);
MyGroupCommand addGroupCommand = new MyGroupCommand() { Caption = "Add", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_16x16.png") };
MyParentCommand parentCommand = new MyParentCommand(viewModel, MyParentCommandType.CommandCreation) { Caption = "Add New Command", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_16x16.png") };
MyParentCommand parentGroup = new MyParentCommand(viewModel, MyParentCommandType.GroupCreation) { Caption = "Add New Group", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_16x16.png") };
MyParentCommand parentPage = new MyParentCommand(viewModel, MyParentCommandType.PageCreation) { Caption = "Add New Page", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_16x16.png") };
addGroupCommand.Commands.Add(parentCommand);
addGroupCommand.Commands.Add(parentGroup);
addGroupCommand.Commands.Add(parentPage);
addingGroup.Commands.Add(addGroupCommand);
addingGroup.Commands.Add(parentCommand);
addingGroup.Commands.Add(parentGroup);
addingGroup.Commands.Add(parentPage);
viewModel.MenuItems.Add(parentCommand);
viewModel.MenuItems.Add(parentGroup);
viewModel.MenuItems.Add(parentPage);
}
#region CommandFuncs
public void cutCommandExecuteFunc() {
Clipboard.SetText(textBox.SelectedText);
textBox.SelectedText = String.Empty;
}
public void copyCommandExecuteFunc() {
Clipboard.SetText(textBox.SelectedText);
}
public void pasteCommandExecuteFunc() {
textBox.SelectedText = Clipboard.GetText();
textBox.SelectionStart += textBox.SelectionLength;
textBox.SelectionLength = 0;
}
public void selectAllCommandExecuteFunc() {
textBox.SelectionStart = 0;
textBox.SelectionLength = textBox.Text.Count();
}
public void blankCommandExecuteFunc() {
textBox.SelectionStart = 0;
textBox.SelectionLength = textBox.Text.Count();
textBox.SelectedText = "";
}
#endregion
}
public class GlyphHelper {
public static ImageSource GetGlyph(string ItemPath) {
return new BitmapImage(AssemblyHelper.GetResourceUri(typeof(MVVMRibbon).Assembly, ItemPath));
}
}
public class ViewModel : DependencyObject {
public static readonly DependencyProperty CategoriesProperty =
DependencyProperty.Register("Categories", typeof(ObservableCollection<CategoryModel>), typeof(ViewModel), new PropertyMetadata(null));
public static readonly DependencyProperty MenuItemsProperty =
DependencyProperty.Register("MenuItems", typeof(ObservableCollection<CommandModel>), typeof(ViewModel), new PropertyMetadata(null));
public ObservableCollection<CategoryModel> Categories {
get { return (ObservableCollection<CategoryModel>)GetValue(CategoriesProperty); }
set { SetValue(CategoriesProperty, value); }
}
public ObservableCollection<CommandModel> MenuItems {
get { return (ObservableCollection<CommandModel>)GetValue(MenuItemsProperty); }
set { SetValue(MenuItemsProperty, value); }
}
public ViewModel() {
Categories = new ObservableCollection<CategoryModel>();
MenuItems = new ObservableCollection<CommandModel>();
}
public void Clear() {
foreach(CategoryModel cat in Categories) {
cat.Clear();
}
Categories.Clear();
}
}
public class ModelBase : DependencyObject {
public static readonly DependencyProperty NameProperty;
static ModelBase() {
NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(ModelBase), new PropertyMetadata(""));
}
public string Name {
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
}
public class CategoryModel : ModelBase {
public static readonly DependencyProperty PagesProperty =
DependencyProperty.Register("Pages", typeof(ObservableCollection<PageModel>), typeof(CategoryModel), new PropertyMetadata(null));
public ObservableCollection<PageModel> Pages {
get { return (ObservableCollection<PageModel>)GetValue(PagesProperty); }
set { SetValue(PagesProperty, value); }
}
public CategoryModel() {
Pages = new ObservableCollection<PageModel>();
}
public void Clear() {
foreach(PageModel cat in Pages) {
cat.Clear();
}
Pages.Clear();
}
}
public class PageModel : ModelBase {
public static readonly DependencyProperty GroupsProperty;
static PageModel() {
GroupsProperty = DependencyProperty.Register("Groups", typeof(ObservableCollection<PageGroupModel>), typeof(PageModel), new PropertyMetadata(null));
}
public PageModel() {
Groups = new ObservableCollection<PageGroupModel>();
}
public ObservableCollection<PageGroupModel> Groups {
get { return ((ObservableCollection<PageGroupModel>)GetValue(GroupsProperty)); }
set { SetValue(GroupsProperty, value); }
}
public void Clear() {
foreach(PageGroupModel cat in Groups) {
cat.Clear();
}
Groups.Clear();
}
}
public class PageGroupModel : ModelBase, ICommand {
public static readonly DependencyProperty CommandsProperty;
public static readonly DependencyProperty GlyphProperty;
static PageGroupModel() {
CommandsProperty = DependencyProperty.Register("Commands", typeof(ObservableCollection<CommandModel>), typeof(PageGroupModel), new PropertyMetadata(null));
GlyphProperty = DependencyProperty.Register("Glyph", typeof(ImageSource), typeof(PageGroupModel), new PropertyMetadata(null, new PropertyChangedCallback(OnGlyphPropertyChanged)));
}
public PageGroupModel() {
Commands = new ObservableCollection<CommandModel>();
}
public ObservableCollection<CommandModel> Commands {
get { return ((ObservableCollection<CommandModel>)GetValue(CommandsProperty)); }
set { SetValue(CommandsProperty, value); }
}
public ImageSource Glyph {
get { return (ImageSource)GetValue(GlyphProperty); }
set { SetValue(GlyphProperty, value); }
}
protected internal static void OnGlyphPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
((PageGroupModel)d).OnGlyphChanged(e);
}
protected internal void OnGlyphChanged(DependencyPropertyChangedEventArgs e) {
((ImageSource)e.NewValue).Freeze();
}
public void Clear() {
Commands.Clear();
}
#region ICommand
bool b = false;
public bool CanExecute(object parameter) {
if(b == true) CanExecuteChanged.Invoke(this, new EventArgs());
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter) {
MessageBox.Show(Name + "'s command executed");
}
#endregion
}
public class CommandModel : DependencyObject, ICommand {
private Action action;
public static readonly DependencyProperty CaptionProperty;
public static readonly DependencyProperty LargeGlyphProperty;
public static readonly DependencyProperty SmallGlyphProperty;
static CommandModel() {
CaptionProperty = DependencyProperty.Register("Caption", typeof(string), typeof(CommandModel), new PropertyMetadata(""));
LargeGlyphProperty = DependencyProperty.Register("LargeGlyph", typeof(ImageSource), typeof(CommandModel), new PropertyMetadata(null, new PropertyChangedCallback(OnGlyphPropertyChanged)));
SmallGlyphProperty = DependencyProperty.Register("SmallGlyph", typeof(ImageSource), typeof(CommandModel), new PropertyMetadata(null, new PropertyChangedCallback(OnGlyphPropertyChanged)));
}
public CommandModel() {
action = ShowMessageBox;
}
public void ShowMessageBox() {
MessageBox.Show(String.Format("Command \"{0}\" executed", this.Caption));
}
public CommandModel(Action action) {
this.action = action;
}
public string Caption {
get { return (string)GetValue(CaptionProperty); }
set { SetValue(CaptionProperty, value); }
}
public ImageSource LargeGlyph {
get { return (ImageSource)GetValue(LargeGlyphProperty); }
set { SetValue(LargeGlyphProperty, value); }
}
public ImageSource SmallGlyph {
get { return (ImageSource)GetValue(SmallGlyphProperty); }
set { SetValue(SmallGlyphProperty, value); }
}
protected internal static void OnGlyphPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
((CommandModel)d).OnGlyphChanged(e);
}
protected internal void OnGlyphChanged(DependencyPropertyChangedEventArgs e) {
((ImageSource)e.NewValue).Freeze();
}
#region ICommand
bool b = false;
public virtual bool CanExecute(object parameter) {
if(b == true) CanExecuteChanged.Invoke(this, new EventArgs());
return true;
}
public event EventHandler CanExecuteChanged;
public virtual void Execute(object parameter) {
action();
}
#endregion
}
public enum MyParentCommandType { CommandCreation, GroupCreation, PageCreation };
public class MyParentCommand : CommandModel {
ViewModel viewModel;
MyParentCommandType type;
public MyParentCommand(ViewModel viewModel, MyParentCommandType type) {
this.viewModel = viewModel;
this.type = type;
}
public override void Execute(object parameter) {
switch(type) {
case MyParentCommandType.CommandCreation:
CommandCreation();
break;
case MyParentCommandType.GroupCreation:
PageGroupCreation();
break;
case MyParentCommandType.PageCreation:
PageCreation();
break;
}
}
private void PageCreation() {
viewModel.Categories[0].Pages.Add(new PageModel() { Name = "New Page" + IndexCreator.GetIndex() });
}
private void PageGroupCreation() {
viewModel.Categories[0].Pages[0].Groups.Add(new PageGroupModel() { Name = "New Group", Glyph = GlyphHelper.GetGlyph("/Images/Icons/NewViaWizard_32x32.png") });
}
private void CommandCreation() {
CommandModel newCommand = new CommandModel() { Caption = "New Command", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/NewViaWizard_32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/NewViaWizard_16x16.png") };
viewModel.Categories[0].Pages[0].Groups[0].Commands.Add(newCommand);
}
}
public class MyGroupCommand : CommandModel {
public static readonly DependencyProperty CommandsProperty;
public ObservableCollection<CommandModel> Commands {
get { return (ObservableCollection<CommandModel>)GetValue(CommandsProperty); }
set { SetValue(CommandsProperty, value); }
}
static MyGroupCommand() {
CommandsProperty = DependencyProperty.Register("Commands", typeof(ObservableCollection<CommandModel>), typeof(MyGroupCommand), new PropertyMetadata(null));
}
public MyGroupCommand()
: base(emptyFunc) {
Commands = new ObservableCollection<CommandModel>();
}
public static void emptyFunc() { }
}
public class CommandTemplateSelector : DataTemplateSelector {
public override DataTemplate SelectTemplate(object item, DependencyObject container) {
if(item is MyGroupCommand) return (DataTemplate)MVVMRibbon.SharedResources["subItemTemplate"];
return (DataTemplate)MVVMRibbon.SharedResources["itemTemplate"];
}
}
public static class IndexCreator {
static int Value = 0;
public static string GetIndex() {
Value++;
return Value.ToString();
}
public static void Refresh() {
Value = 0;
}
}
}