EManning EManning - 1 year ago 53
Vb.net Question

Cannot properly form xaml for command binding

I'm trying to implement command binding so that my user can use Alt+ for navigation. I found this example but am having problems forming the xmlns and the CommandBinding xaml.

This is what I have so far. I know it's not formed correctly.

xmlns:local="clr-namespace:xxxxx"

<Window.CommandBindings>
<CommandBinding
Command="{x:Static local:xxxxx.ProjectMsg}"
Executed="ProjectMsg"/>
</Window.CommandBindings>


The coding for the command is here in the MainWindow vb:

Class MainWindow


Public Shared ProjectTab_AltP As New RoutedCommand


Public Sub New()

' This call is required by the designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.

End Sub

Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded


ProjectTab_AltP.InputGestures.Add(New KeyGesture(Key.P, ModifierKeys.Control))
Me.CommandBindings.Add(New CommandBinding(ProjectTab_AltP, AddressOf Me.ProjectMsg))

End Sub

Private Sub ProjectMsg(sender As Object, e As ExecutedRoutedEventArgs)

MessageBox.Show("hi")

End Sub


I get an error that the name "xxxxx" does not exist in the namespace clr-namespace:xxxxx.

In the main window vb, there is not a namespace declared. Numerous errors occur if I attempt to add one.

Do I have to have a namespace in the main window vb?

I'm a noob at wpf so I would appreciate any help or advice. Thanks.

mm8 mm8
Answer Source

Do I have to have a namespace in the main window vb?

No. Check what the root namespace of your application is under Project->Properties->Application->Root namespace in Visual Studio. It is the same as the name of your application by default, for example "WpfApplication1":

xmlns:local="clr-namespace:WpfApplication1"

You should also expose the command through a property:

Public Shared ReadOnly Property ProjectTab_AltP() As RoutedUICommand = New RoutedUICommand()

<Window.CommandBindings>
    <CommandBinding
    Command="{x:Static local:MainWindow.ProjectTab_AltP}"
    Executed="ProjectMsg"/>
</Window.CommandBindings>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download