将TextBox绑定到从其他Thread更新的值

时间:2011-04-20 06:13:20

标签: wpf vb.net exception binding

只要按下按钮,我就试图增加一个值。增量值通过绑定到TextBox连接。更新底层整数时出现问题。我得到另一个Thread拥有它的异常。

    <Button Name="Up"
            Content="Up"
            PreviewMouseLeftButtonDown="Up_PreviewMouseLeftButtonDown"
            PreviewMouseLeftButtonUp="Up_PreviewMouseLeftButtonUp">
    </Button>
    <TextBox Text="{Binding NumericField}">

初始化期间:

Timer = New Timers.Timer
Timer.Interval = 100
AddHandler Timer.Elapsed, AddressOf Timer_Elapsed

代码背后:

  Private _numericField As Integer
  Public Property NumericField As Integer
    Get
      Return _numericField
    End Get
    Set(ByVal value As Integer)
      _numericField = value
      RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("NumericField"))
    End Set
  End Property

  Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
    NumericField += 1
  End Sub

  Private Sub Up_PreviewMouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
    Timer.Start()
  End Sub

  Private Sub Up_PreviewMouseLeftButtonUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
    Timer.Stop()
  End Sub

1 个答案:

答案 0 :(得分:2)

这看起来有点太复杂了。你不能使用RepeatButton吗?

简单解决方案:

<RepeatButton Click="RepeatButton_Click" Content="Up" />

代码背后:

  Private Sub RepeatButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    NumericField += 1
  End Sub

使用命令

尽量避免代码隐藏文件中不必要的代码。这是使用命令草图

XAML:

<RepeatButton Command="{Binding IncrementField}" Content="Up"></RepeatButton>

帮助上课。可重复使用项目中的所有命令:

Public Class ActionCommand
  Implements ICommand

  Private ReadOnly _executeHandler As Action(Of Object)
  Private ReadOnly _canExecuteHandler As Func(Of Object, Boolean)

  Public Sub New(ByVal execute As Action(Of Object),
                 ByVal canExecute As Func(Of Object, Boolean))
    If execute Is Nothing Then
      Throw New ArgumentNullException("Execute cannot be null")
    End If
    _executeHandler = execute
    _canExecuteHandler = canExecute
  End Sub

  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 Object, ByVal e As System.EventArgs)
      CommandManager.InvalidateRequerySuggested()
    End RaiseEvent
  End Event

  Public Sub Execute(ByVal parameter As Object) Implements ICommand.Execute
    _executeHandler(parameter)
  End Sub

  Public Function CanExecute(ByVal parameter As Object) As Boolean Implements ICommand.CanExecute
    If (_canExecuteHandler Is Nothing) Then
      Return True
    End If
    Return _canExecuteHandler(parameter)
  End Function
End Class

在你的模特中:

_incrementField = New ActionCommand(AddressOf IncrementExecuted, AddressOf IncrementCanExecute)

...

  Private Function IncrementCanExecute(ByVal parameter As Object) As Boolean
    Return True
  End Function

  Private Sub IncrementExecuted(ByVal parameter As Object)
    NumericField += 1
  End Sub

  Private _incrementField As ActionCommand
  Public ReadOnly Property IncrementField As ActionCommand
    Get
      Return _incrementField
    End Get
  End Property