我想使用MVVM模式实现点击事件。我完成的代码如下:-
在xaml文件中:-
添加 namespace
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
然后添加
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ButtonClickCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
,并从按钮中删除了点击事件
在xaml.vb页面内
Public Sub New()
InitializeComponent()
Me.DataContext = New CoverageViewModel
End Sub
在我的 View 模型页面内
Public ReadOnly Property ButtonClickCommand() As ICommand
Get
Return New DelegateCommand(Of Object)(AddressOf ButtonClicked)
End Get
End Property
Public Sub ButtonClicked(obj As Object)
Dim class As New Myclass
If (Globals.IsEdited = False) Then
'My Code
End If
End Sub
但是它不起作用,请告诉我应该在代码中进行的更改。
谢谢
最佳答案
您确实想为此使用relaycommand。
首先将以下内容添加到您的项目中:
Imports System.Windows.Input
''' <summary>
''' A command whose sole purpose is to
''' relay its functionality to other
''' objects by invoking delegates. The
''' default return value for the CanExecute
''' method is 'true'.
''' </summary>
Public Class RelayCommand
Implements ICommand
#Region "Fields"
Private ReadOnly _execute As Action(Of Object)
Private ReadOnly _canExecute As Predicate(Of Object)
#End Region ' Fields
#Region "Constructors"
''' <summary>
''' Creates a new command that can always execute.
''' </summary>
''' <param name="execute">The execution logic.</param>
Public Sub New(ByVal execute As Action(Of Object))
Me.New(execute, Nothing)
End Sub
''' <summary>
''' Creates a new command.
''' </summary>
''' <param name="execute">The execution logic.</param>
''' <param name="canExecute">The execution status logic.</param>
Public Sub New(ByVal execute As Action(Of Object), ByVal canExecute As Predicate(Of Object))
If execute Is Nothing Then
Throw New ArgumentNullException("execute")
End If
_execute = execute
_canExecute = canExecute
End Sub
#End Region ' Constructors
#Region "ICommand Members"
<DebuggerStepThrough> _
Public Function CanExecute(ByVal parameter As Object) As Boolean Implements ICommand.CanExecute
Return If(_canExecute Is Nothing, True, _canExecute(parameter))
End Function
Public Custom Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
AddHandler(ByVal value As EventHandler)
AddHandler CommandManager.RequerySuggested, value
End AddHandler
RemoveHandler(ByVal value As EventHandler)
RemoveHandler CommandManager.RequerySuggested, value
End RemoveHandler
RaiseEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
End RaiseEvent
End Event
Public Sub Execute(ByVal parameter As Object) Implements ICommand.Execute
_execute(parameter)
End Sub
#End Region ' ICommand Members
End Class
现在在您的xaml中,将以下内容放入描述您的按钮的xaml中
Command={Binding Path=MyButtonClickCommand}
现在,在您的 View 用作其数据上下文的 View 模型中,添加以下内容:
Private _myButtonClickCommand As ICommand
Public ReadOnly Property MyButtonClickCommand As ICommand
Get
if _myButtonClickCommand Is Nothing Then
Dim myMyButtonClick As New Action(Of Object)(AddressOf MyButtonClick)
_myButtonClickCommand = New RelayCommand(myMyButtonClick)
End If
Return _myButtonClickCommand
End Get
End Property
Private Sub MyButtonClick(ByVal obj As Object)
'Add code here to do want you want doing when the button is clicked
End Sub
您可以将此原则应用于各种事物。我强烈建议您阅读有关here的Josh Smiths权威文章
关于wpf - 使用mvvm实现点击事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30891783/