lxn / walk

A Windows GUI toolkit for the Go Programming Language
Other
6.83k stars 884 forks source link

How can I bind a variable to a Text field ? #754

Closed alkanna closed 3 years ago

alkanna commented 3 years ago

Let's say I have a StatusBarItem, I would like to be able to bind a variable (currently a global struct, holding some data that is meant to vary) so that the text always reflects the value of this variable.

For example, imagine I have a struct :

type Player struct {
    PlayerExperience int
}

var player Player

Can I do something like this ?

func main() {
    ...
    StatusBarItems: []StatusBarItem{
            {
                Text: Bind(player.PlayerExperience),
            },
        }
    ...
}

I have a hard time figuring it out since there is little doc on the subject :( Do I have to manually reinject the value into the StatusBarItem every time I get a new value for it ?

lxn commented 3 years ago

Data binding in walk works by Binding an expression to a declarative struct field of type Property. To be able to access some data like an instance of your Player struct, you would use a DataBinder.

Please have a look at the databinding, gradientcomposite and webview examples.

In case of StatusBarItem you indeed would have to update the text manually. Data binding in general is limited to stuff embedding WindowBase.

alkanna commented 3 years ago

Thank you very much, that's what I was expecting for the StatusBarItem seeing as its Text field takes a string and not a Property.