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.Utils;
namespace BarsDemo {
public partial class MVVMBar : BarsDemoModule {
private static ResourceDictionary sharedResources;
public static ResourceDictionary SharedResources {
get { return sharedResources; }
set { sharedResources = value; }
}
public MVVMBar() {
InitializeComponent();
SharedResources = Resources;
InitializeViewModel(textBox);
}
protected override void OnUnloaded(object sender, RoutedEventArgs e) {
SharedResources = null;
base.OnUnloaded(sender, e);
}
private void InitializeViewModel(TextBox textBox) {
ViewModel viewModel = new ViewModel();
BarModel clipboardBar = new BarModel() { Name = "Clipboard" };
BarModel addingBar = new BarModel() { Name = "Addition" };
viewModel.Bars.Add(clipboardBar);
viewModel.Bars.Add(addingBar);
MyCommand cutCommand = new MyCommand(cutCommandExecuteFunc) { Caption = "Cut", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/Cut_32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/Cut_16x16.png") };
MyCommand copyCommand = new MyCommand(copyCommandExecuteFunc) { Caption = "Copy", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/Copy_32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/Copy_16x16.png") };
MyCommand pasteCommand = new MyCommand(pasteCommandExecuteFunc) { Caption = "Paste", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/Paste_32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/Paste_16x16.png") };
MyCommand selectCommand = new MyCommand(selectAllCommandExecuteFunc) { Caption = "Select All", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/SelectAll_32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/SelectAll_16x16.png") };
MyCommand blankCommand = new MyCommand(blankCommandExecuteFunc) { Caption = "Clear Page", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/New_32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/New_16x16.png") };
clipboardBar.Commands.Add(cutCommand);
clipboardBar.Commands.Add(copyCommand);
clipboardBar.Commands.Add(pasteCommand);
clipboardBar.Commands.Add(selectCommand);
clipboardBar.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 Command", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_16x16.png") };
MyParentCommand parentBar = new MyParentCommand(viewModel, MyParentCommandType.BarCreation) { Caption = "Add Bar", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_16x16.png") };
addGroupCommand.Commands.Add(parentCommand);
addGroupCommand.Commands.Add(parentBar);
addingBar.Commands.Add(addGroupCommand);
addingBar.Commands.Add(parentCommand);
addingBar.Commands.Add(parentBar);
DataContext = viewModel;
}
#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(MVVMBar).Assembly, ItemPath));
}
}
public class ViewModel : DependencyObject {
public static readonly DependencyProperty BarsProperty =
DependencyProperty.Register("Bars", typeof(ObservableCollection<BarModel>), typeof(ViewModel), new PropertyMetadata(null));
public ObservableCollection<BarModel> Bars {
get { return (ObservableCollection<BarModel>)GetValue(BarsProperty); }
set { SetValue(BarsProperty, value); }
}
public ViewModel() {
Bars = new ObservableCollection<BarModel>();
}
private ObservableCollection<BarModel> CreateCollectionDublicates(ObservableCollection<BarModel> Groups) {
return null;
}
}
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 BarModel : ModelBase {
public static readonly DependencyProperty CommandsProperty;
static BarModel() {
CommandsProperty = DependencyProperty.Register("Commands", typeof(ObservableCollection<MyCommand>), typeof(BarModel), new PropertyMetadata(null));
}
public BarModel() {
Commands = new ObservableCollection<MyCommand>();
}
public ObservableCollection<MyCommand> Commands {
get { return ((ObservableCollection<MyCommand>)GetValue(CommandsProperty)); }
set { SetValue(CommandsProperty, value); }
}
}
public class MyCommand : DependencyObject, ICommand {
Action action;
public static readonly DependencyProperty CaptionProperty;
public static readonly DependencyProperty LargeGlyphProperty;
public static readonly DependencyProperty SmallGlyphProperty;
static MyCommand() {
CaptionProperty = DependencyProperty.Register("Caption", typeof(string), typeof(MyCommand), new PropertyMetadata(""));
LargeGlyphProperty = DependencyProperty.Register("LargeGlyph", typeof(ImageSource), typeof(MyCommand), new PropertyMetadata(null));
SmallGlyphProperty = DependencyProperty.Register("SmallGlyph", typeof(ImageSource), typeof(MyCommand), new PropertyMetadata(null));
}
public MyCommand() {
}
private void ShowMSGBX() {
MessageBox.Show(String.Format("Command \"{0}\" executed", this.Caption));
}
public MyCommand(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); }
}
#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 virtual void Execute(object parameter) {
if(action != null)
action();
else
ShowMSGBX();
}
#endregion
}
public enum MyParentCommandType { CommandCreation, BarCreation };
public class MyParentCommand : MyCommand {
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.BarCreation:
BarCreation();
break;
}
}
private MyCommand CreateCommand() {
return new MyCommand() { Caption = "New Command", LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/NewViaWizard_32x32.png"), SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/NewViaWizard_16x16.png") };
}
private void BarCreation() {
BarModel model = new BarModel() { Name = "New Bar" };
model.Commands.Add(CreateCommand());
viewModel.Bars.Add(model);
}
private void CommandCreation() {
viewModel.Bars[0].Commands.Add(CreateCommand());
}
}
public class MyGroupCommand : MyCommand {
public static readonly DependencyProperty CommandsProperty;
public ObservableCollection<MyCommand> Commands {
get { return (ObservableCollection<MyCommand>)GetValue(CommandsProperty); }
set { SetValue(CommandsProperty, value); }
}
static MyGroupCommand() {
CommandsProperty = DependencyProperty.Register("Commands", typeof(ObservableCollection<MyCommand>), typeof(MyGroupCommand), new PropertyMetadata(null));
}
public MyGroupCommand()
: base(emptyFunc) {
Commands = new ObservableCollection<MyCommand>();
}
public static void emptyFunc() {
}
}
public class CommandTemplateSelector : DataTemplateSelector {
public override DataTemplate SelectTemplate(object item, DependencyObject container) {
if(item is MyGroupCommand) return (DataTemplate)MVVMBar.SharedResources["subItemTemplate"];
return (DataTemplate)MVVMBar.SharedResources["itemTemplate"];
}
}
}