Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/WPF/VB/BarsDemo.Wpf/Modules/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/WPF/VB/BarsDemo.Wpf/Modules/MVVMBar.xaml.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Collections.ObjectModel
Imports DevExpress.Xpf.Bars
Imports System.Collections.Specialized
Imports DevExpress.Utils

Namespace BarsDemo
	Partial Public Class MVVMBar
		Inherits BarsDemoModule
		Private Shared sharedResources_Renamed As ResourceDictionary
		Public Shared Property SharedResources() As ResourceDictionary
			Get
				Return sharedResources_Renamed
			End Get
			Set(ByVal value As ResourceDictionary)
				sharedResources_Renamed = value
			End Set
		End Property

		Public Sub New()
			InitializeComponent()
			SharedResources = Resources
			InitializeViewModel(textBox)
		End Sub

		Protected Overrides Sub OnUnloaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
			SharedResources = Nothing
			MyBase.OnUnloaded(sender, e)
		End Sub

	   Private Sub InitializeViewModel(ByVal textBox As TextBox)
			Dim viewModel As New ViewModel()
			Dim clipboardBar As New BarModel() With {.Name = "Clipboard"}
			Dim addingBar As New BarModel() With {.Name = "Addition"}

			viewModel.Bars.Add(clipboardBar)
			viewModel.Bars.Add(addingBar)
			Dim cutCommand As New MyCommand(AddressOf cutCommandExecuteFunc) With {.Caption = "Cut", .LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/Cut_32x32.png"), .SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/Cut_16x16.png")}
			Dim copyCommand As New MyCommand(AddressOf copyCommandExecuteFunc) With {.Caption = "Copy", .LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/Copy_32x32.png"), .SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/Copy_16x16.png")}
			Dim pasteCommand As New MyCommand(AddressOf pasteCommandExecuteFunc) With {.Caption = "Paste", .LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/Paste_32x32.png"), .SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/Paste_16x16.png")}
			Dim selectCommand As New MyCommand(AddressOf selectAllCommandExecuteFunc) With {.Caption = "Select All", .LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/SelectAll_32x32.png"), .SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/SelectAll_16x16.png")}
			Dim blankCommand As New MyCommand(AddressOf blankCommandExecuteFunc) With {.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)
			Dim addGroupCommand As New MyGroupCommand() With {.Caption = "Add", .LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_32x32.png"), .SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_16x16.png")}
			Dim parentCommand As New MyParentCommand(viewModel, MyParentCommandType.CommandCreation) With {.Caption = "Add Command", .LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_32x32.png"), .SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/Add_16x16.png")}
			Dim parentBar As New MyParentCommand(viewModel, MyParentCommandType.BarCreation) With {.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
	   End Sub
	   #Region "CommandFuncs"
	   Public Sub cutCommandExecuteFunc()
		   Clipboard.SetText(textBox.SelectedText)
		   textBox.SelectedText = String.Empty
	   End Sub
	   Public Sub copyCommandExecuteFunc()
		   Clipboard.SetText(textBox.SelectedText)
	   End Sub
	   Public Sub pasteCommandExecuteFunc()
		   textBox.SelectedText = Clipboard.GetText()
		   textBox.SelectionStart += textBox.SelectionLength
		   textBox.SelectionLength = 0
	   End Sub
	   Public Sub selectAllCommandExecuteFunc()
		   textBox.SelectionStart = 0
		   textBox.SelectionLength = textBox.Text.Count()
	   End Sub
	   Public Sub blankCommandExecuteFunc()
		   textBox.SelectionStart = 0
		   textBox.SelectionLength = textBox.Text.Count()
		   textBox.SelectedText = ""
	   End Sub
		#End Region

	End Class
	Public Class GlyphHelper
		Public Shared Function GetGlyph(ByVal ItemPath As String) As ImageSource
			Return New BitmapImage(AssemblyHelper.GetResourceUri(GetType(MVVMBar).Assembly, ItemPath))
		End Function
	End Class
	Public Class ViewModel
		Inherits DependencyObject
		Public Shared ReadOnly BarsProperty As DependencyProperty = DependencyProperty.Register("Bars", GetType(ObservableCollection(Of BarModel)), GetType(ViewModel), New PropertyMetadata(Nothing))

		Public Property Bars() As ObservableCollection(Of BarModel)
			Get
				Return CType(GetValue(BarsProperty), ObservableCollection(Of BarModel))
			End Get
			Set(ByVal value As ObservableCollection(Of BarModel))
				SetValue(BarsProperty, value)
			End Set
		End Property

		Public Sub New()
			Bars = New ObservableCollection(Of BarModel)()
		End Sub
		Private Function CreateCollectionDublicates(ByVal Groups As ObservableCollection(Of BarModel)) As ObservableCollection(Of BarModel)
			Return Nothing
		End Function
	End Class
	Public Class ModelBase
		Inherits DependencyObject
		Public Shared ReadOnly NameProperty As DependencyProperty

		Shared Sub New()
			NameProperty = DependencyProperty.Register("Name", GetType(String), GetType(ModelBase), New PropertyMetadata(""))
		End Sub
		Public Property Name() As String
			Get
				Return CStr(GetValue(NameProperty))
			End Get
			Set(ByVal value As String)
				SetValue(NameProperty, value)
			End Set
		End Property
	End Class
	Public Class BarModel
		Inherits ModelBase
		Public Shared ReadOnly CommandsProperty As DependencyProperty

		Shared Sub New()
			CommandsProperty = DependencyProperty.Register("Commands", GetType(ObservableCollection(Of MyCommand)), GetType(BarModel), New PropertyMetadata(Nothing))
		End Sub
		Public Sub New()
			Commands = New ObservableCollection(Of MyCommand)()
		End Sub
		Public Property Commands() As ObservableCollection(Of MyCommand)
			Get
				Return (CType(GetValue(CommandsProperty), ObservableCollection(Of MyCommand)))
			End Get
			Set(ByVal value As ObservableCollection(Of MyCommand))
				SetValue(CommandsProperty, value)
			End Set
		End Property
	End Class
	Public Class MyCommand
		Inherits DependencyObject
		Implements ICommand
		Private action As Action
		Public Shared ReadOnly CaptionProperty As DependencyProperty
		Public Shared ReadOnly LargeGlyphProperty As DependencyProperty
		Public Shared ReadOnly SmallGlyphProperty As DependencyProperty

		Shared Sub New()
			CaptionProperty = DependencyProperty.Register("Caption", GetType(String), GetType(MyCommand), New PropertyMetadata(""))
			LargeGlyphProperty = DependencyProperty.Register("LargeGlyph", GetType(ImageSource), GetType(MyCommand), New PropertyMetadata(Nothing))
			SmallGlyphProperty = DependencyProperty.Register("SmallGlyph", GetType(ImageSource), GetType(MyCommand), New PropertyMetadata(Nothing))
		End Sub
		Public Sub New()

		End Sub
		Private Sub ShowMSGBX()
			MessageBox.Show(String.Format("Command ""{0}"" executed", Me.Caption))
		End Sub
		Public Sub New(ByVal action As Action)
				Me.action = action
		End Sub

		Public Property Caption() As String
			Get
				Return CStr(GetValue(CaptionProperty))
			End Get
			Set(ByVal value As String)
				SetValue(CaptionProperty, value)
			End Set
		End Property
		Public Property LargeGlyph() As ImageSource
			Get
				Return CType(GetValue(LargeGlyphProperty), ImageSource)
			End Get
			Set(ByVal value As ImageSource)
				SetValue(LargeGlyphProperty, value)
			End Set
		End Property
		Public Property SmallGlyph() As ImageSource
			Get
				Return CType(GetValue(SmallGlyphProperty), ImageSource)
			End Get
			Set(ByVal value As ImageSource)
				SetValue(SmallGlyphProperty, value)
			End Set
		End Property

		#Region "ICommand"
		Private b As Boolean = False
		Public Function CanExecute(ByVal parameter As Object) As Boolean Implements ICommand.CanExecute
			If b=True Then
				CanExecuteChangedEvent.Invoke(Me, New EventArgs())
			End If
			Return True
		End Function

		Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged

		Public Overridable Sub Execute(ByVal parameter As Object) Implements ICommand.Execute
			If action IsNot Nothing Then
				action()
			Else
				ShowMSGBX()
			End If
		End Sub
		#End Region
	End Class
	Public Enum MyParentCommandType
		CommandCreation
		BarCreation
	End Enum
	Public Class MyParentCommand
		Inherits MyCommand
		Private viewModel As ViewModel
		Private type As MyParentCommandType
		Public Sub New(ByVal viewModel As ViewModel, ByVal type As MyParentCommandType)
			Me.viewModel = viewModel
			Me.type = type
		End Sub

	 Public Overrides Sub Execute(ByVal parameter As Object)
			Select Case type
				Case MyParentCommandType.CommandCreation
					CommandCreation()
				Case MyParentCommandType.BarCreation
					BarCreation()
			End Select
	 End Sub
		Private Function CreateCommand() As MyCommand
			Return New MyCommand() With {.Caption = "New Command", .LargeGlyph = GlyphHelper.GetGlyph("/Images/Icons/NewViaWizard_32x32.png"), .SmallGlyph = GlyphHelper.GetGlyph("/Images/Icons/NewViaWizard_16x16.png")}
		End Function
		Private Sub BarCreation()
			Dim model As New BarModel() With {.Name = "New Bar"}
			model.Commands.Add(CreateCommand())
			viewModel.Bars.Add(model)
		End Sub

		Private Sub CommandCreation()
			viewModel.Bars(0).Commands.Add(CreateCommand())
		End Sub
	End Class
	Public Class MyGroupCommand
		Inherits MyCommand
		Public Shared ReadOnly CommandsProperty As DependencyProperty

		Public Property Commands() As ObservableCollection(Of MyCommand)
			Get
				Return CType(GetValue(CommandsProperty), ObservableCollection(Of MyCommand))
			End Get
			Set(ByVal value As ObservableCollection(Of MyCommand))
				SetValue(CommandsProperty, value)
			End Set
		End Property
		Shared Sub New()
			CommandsProperty = DependencyProperty.Register("Commands", GetType(ObservableCollection(Of MyCommand)), GetType(MyGroupCommand), New PropertyMetadata(Nothing))
		End Sub
		Public Sub New()
			MyBase.New(AddressOf emptyFunc)
				Commands = New ObservableCollection(Of MyCommand)()
		End Sub
		Public Shared Sub emptyFunc()
		End Sub
	End Class
	Public Class CommandTemplateSelector
		Inherits DataTemplateSelector
		Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
			If TypeOf item Is MyGroupCommand Then
				Return CType(MVVMBar.SharedResources("subItemTemplate"), DataTemplate)
			End If
			Return CType(MVVMBar.SharedResources("itemTemplate"), DataTemplate)
		End Function
	End Class
End Namespace