Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions api/v2.5/data/binding/datalistener.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,53 @@ NewDataListener is a helper function that creates a new listener type from a sim

<div class="since">Since: <code>
2.0</code></div>

### Example

```go
package fynedemo

import (
"testing"

"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/test"
"fyne.io/fyne/v2/widget"
)

type ViewModel struct {
IsStarted binding.Bool
}

func (vm *ViewModel) Start() {
vm.IsStarted.Set(true)
}

func TestStartButtonConnectedToViewModelByAListener(t *testing.T) {

vm := ViewModel{
IsStarted: binding.NewBool(),
}
vm.IsStarted.Set(false)

startButton := widget.NewButton("Start", vm.Start)

vm.IsStarted.AddListener(binding.NewDataListener(
func() {

isStarted, _ := vm.IsStarted.Get()
if isStarted {
startButton.Disable()
} else {
startButton.Enable()
}

if startButton.Disabled() && !isStarted {
t.Errorf("StartButton.Disabled(): `%v` want `%v`", startButton.Disabled(), !startButton.Disabled())
}
},
))

test.Tap(startButton)
}
```