WPF學習日誌 ~ ApplicationCommand ~

在WPF裡面有ApplicationCommand的功能可以讓我們來利用;比如說在做文字編輯的時候,會用到的剪下、貼上,等等的功能,在以往我們要撰寫一些對應的程式碼,除了功能的部分,在Enable/Disable相關的動作,我們也必須要去撰寫相關的程式碼,在WPF裡面我們可以很輕鬆的做到這些動作,你可以在執行過程中,觀看Enable/Disable的變化,對於相關的動作,我們可是沒有撰寫相關的程式碼喔

在WPF裡面有ApplicationCommand的功能可以讓我們來利用;比如說在做文字編輯的時候,會用到的剪下、貼上,等等的功能,在以往我們要撰寫一些對應的程式碼,除了功能的部分,在Enable/Disable相關的動作,我們也必須要去撰寫相關的程式碼,在WPF裡面我們可以很輕鬆的做到這些動作,你可以在執行過程中,觀看Enable/Disable的變化,對於相關的動作,我們可是沒有撰寫相關的程式碼喔,下面我們來看看測試的程式碼吧

 

    Dim dp As DockPanel
    Dim tb As ToolBar
    Dim tbTray As ToolBarTray
    Dim txtBody As TextBox

    Dim btnCopy As Button
    Dim btnPaste As Button
    Dim btnCut As Button

    Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        dp = New DockPanel
        Me.Content = dp
        tbTray = New ToolBarTray
        dp.Children.Add(tbTray)
        DockPanel.SetDock(tbTray, Dock.Top)
        txtBody = New TextBox
        txtBody.AcceptsReturn = True
        txtBody.Background = Brushes.AliceBlue
        txtBody.VerticalScrollBarVisibility = ScrollBarVisibility.Auto
        dp.Children.Add(txtBody)
        ''設定最後加入的項目填滿剩餘的空間
        dp.LastChildFill = True
        ''設定剪下、貼上等Command
        btnCopy = New Button
        btnCopy.Content = "Copy"
        ''設定對應的Command
        btnCopy.Command = ApplicationCommands.Copy
        btnPaste = New Button
        btnPaste.Content = "Paset"
        btnPaste.Command = ApplicationCommands.Paste
        btnCut = New Button
        btnCut.Content = "Cut"
        btnCut.Command = ApplicationCommands.Cut
        tb = New ToolBar
        tb.Items.Add(btnCopy)
        tb.Items.Add(New Separator)
        tb.Items.Add(btnPaste)
        tb.Items.Add(New Separator)
        tb.Items.Add(btnCut)
        tbTray.ToolBars.Add(tb)
        ''第二個Toolbar
        Dim tb2 As New ToolBar
        Dim btnUnDo As New Button
        btnUnDo.Content = "UnDo"
        btnUnDo.Command = ApplicationCommands.Undo
        Dim btnReDo As New Button
        btnReDo.Content = "ReDo"
        btnReDo.Command = ApplicationCommands.Redo
        tb2.Items.Add(btnUnDo)
        tb2.Items.Add(New Separator)
        tb2.Items.Add(btnReDo)
        tbTray.ToolBars.Add(tb2)
    End Sub
End Class