开发者

Command handling in Composite WPF module without framework

I am doing a module in an application where I need to create a complex UI. I decided to use user controls for the UI because of the complexity.

Because of the composite nature of this, I am facing some challenges. There is a usercontrol which can contain another user control.

<MainWindow>

  <UserSelectionUserControl>
     <Button Command="SelectFirstChild/>
     <Button Command="SelectSecondChild/>
     <Button Command="SelectThirdChild/>
  </UserSelectionUserControl>


   <MainUserControl>
      <ChildUserControl/>
   </MainUserControl>

</MainWindow>

There are several ChildUserControls. At runtime I have to attach ChildUserControl in the MainUserControl according to user button click.

My problem is-how can I do the messaging effectively? I cannot use event agggregation/unity because I am just doing a module in a large application. I could use RoutedCommand , but is it possible to pass par开发者_StackOverflowameters with the command. For example all the buttons will fire the same command with some string value which uniquely identifies the button clicked?


Yes, you can use parameters with the command. In the UserControls declaration, use the CommandParameter property to bind to or define the data that you want sent to the command. your Command declaration will have to provide for an object to be passed in by value.

<Button Content="Browse" Command="{Binding BrowseCommand}" CommandParameter="Image"/>

-OR-

<Button Content="Browse" Command="{Binding BrowseCommand}" CommandParameter="{Binding SelectedItem, ElementName=listBox}"/>

In application where I am trying to limit the framework usage, I use MVVMFoundation, and my Command Properties and Command Methods look like this:

 Private _cmdBrowseCommand As ICommand
Public ReadOnly Property BrowseCommand() As ICommand
    Get
        If _cmdBrowseCommand Is Nothing Then
            _cmdBrowseCommand = New RelayCommand(Of Object)(AddressOf BrowseExecute)
        End If
        Return _cmdBrowseCommand
    End Get

End Property

Private Sub BrowseExecute(ByVal param As Object)
    If TypeOf(param) is PannableImage Then
        'Code removed for brevity
    End If
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜