开发者

Binding TextBox to a value that is updated from a different Thread

开发者 https://www.devze.com 2023-02-27 10:19 出处:网络
I tried to increment a value as long as I press a button. The incremented value is connected via binding to a TextBox. The problem occures when I update the underlaying integer. I get an exception tha

I tried to increment a value as long as I press a button. The incremented value is connected via binding to a TextBox. The problem occures when I update the underlaying integer. I get an exception that another Thread owns it.

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

During Initialisation:

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

Code behind:

  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


This looks a bit too complicated. Can't you use a RepeatButton?

Simple solution:

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

Code behind:

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

Using commands

Try to avoid unnecessary code in your code behind file. Here's a sketch using Commands

XAML:

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

Helping class. Re-usable for all commands inside your project:

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

And in your model:

_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
0

精彩评论

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