Go GUI---lxn/walk 自带demo学习---3.数据绑定 databinding
这个demo纵向布局,从上到下是一个PushButton、一个Label、一个TextEdit。点击名称为Eidt Animal的按钮弹出自定义的对话框,进行编辑,编辑完成后点击OK按钮,TextEdit用于显示编辑的内容。databinding.go// Copyright 2013 The Walk Authors. All rights reserved.// Use of this sou
·
这个demo纵向布局,从上到下是一个PushButton、一个Label、一个TextEdit。
点击名称为Eidt Animal的按钮弹出自定义的对话框,进行编辑,编辑完成后点击OK按钮,TextEdit用于显示编辑的内容。
其中涉及的几个控件简单说一下:
DateEdit :日期控件,只有年月日,没有时分秒。
RadioButtonGroupBox和RadioButton : 分组框和单选按钮
ComboBox : 下拉列表框 Editable:true 可以编辑
CheckBox : 复选框
至于Bind方法,就是将我们填写或者选择的数据与Animal结构体中定义的变量绑定
databinding.go
// Copyright 2013 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"log"
"time"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
func main() {
walk.AppendToWalkInit(func() {
walk.FocusEffect, _ = walk.NewBorderGlowEffect(walk.RGB(0, 63, 255))
walk.InteractionEffect, _ = walk.NewDropShadowEffect(walk.RGB(63, 63, 63))
walk.ValidationErrorEffect, _ = walk.NewBorderGlowEffect(walk.RGB(255, 0, 0))
})
var mw *walk.MainWindow
var outTE *walk.TextEdit
animal := new(Animal)
if _, err := (MainWindow{
AssignTo: &mw,
Title: "Walk Data Binding Example",
MinSize: Size{300, 200},
Layout: VBox{},
Children: []Widget{
PushButton{
Text: "Edit Animal",
OnClicked: func() {
if cmd, err := RunAnimalDialog(mw, animal); err != nil {
log.Print(err)
} else if cmd == walk.DlgCmdOK {
outTE.SetText(fmt.Sprintf("%+v", animal))
}
},
},
Label{
Text: "animal:",
},
TextEdit{
AssignTo: &outTE,
ReadOnly: true,
Text: fmt.Sprintf("%+v", animal),
},
},
}.Run()); err != nil {
log.Fatal(err)
}
}
type Animal struct {
Name string
ArrivalDate time.Time
SpeciesId int
Speed int
Sex Sex
Weight float64
PreferredFood string
Domesticated bool
Remarks string
Patience time.Duration
}
func (a *Animal) PatienceField() *DurationField {
return &DurationField{&a.Patience}
}
type Species struct {
Id int
Name string
}
func KnownSpecies() []*Species {
return []*Species{
{1, "Dog"},
{2, "Cat"},
{3, "Bird"},
{4, "Fish"},
{5, "Elephant"},
}
}
type DurationField struct {
p *time.Duration
}
func (*DurationField) CanSet() bool { return true }
func (f *DurationField) Get() interface{} { return f.p.String() }
func (f *DurationField) Set(v interface{}) error {
x, err := time.ParseDuration(v.(string))
if err == nil {
*f.p = x
}
return err
}
func (f *DurationField) Zero() interface{} { return "" }
type Sex byte
const (
SexMale Sex = 1 + iota
SexFemale
SexHermaphrodite
)
func RunAnimalDialog(owner walk.Form, animal *Animal) (int, error) {
var dlg *walk.Dialog
var db *walk.DataBinder
var acceptPB, cancelPB *walk.PushButton
return Dialog{
AssignTo: &dlg,
Title: Bind("'Animal Details' + (animal.Name == '' ? '' : ' - ' + animal.Name)"),
DefaultButton: &acceptPB,
CancelButton: &cancelPB,
DataBinder: DataBinder{
AssignTo: &db,
Name: "animal",
DataSource: animal,
ErrorPresenter: ToolTipErrorPresenter{},
},
MinSize: Size{300, 300},
Layout: VBox{},
Children: []Widget{
Composite{
Layout: Grid{Columns: 2},
Children: []Widget{
Label{
Text: "Name:",
},
LineEdit{
Text: Bind("Name"),
},
Label{
Text: "Arrival Date:",
},
DateEdit{
Date: Bind("ArrivalDate"),
},
Label{
Text: "Species:",
},
ComboBox{
Value: Bind("SpeciesId", SelRequired{}),
BindingMember: "Id",
DisplayMember: "Name",
Model: KnownSpecies(),
},
Label{
Text: "Speed:",
},
Slider{
Value: Bind("Speed"),
},
RadioButtonGroupBox{
ColumnSpan: 2,
Title: "Sex",
Layout: HBox{},
DataMember: "Sex",
Buttons: []RadioButton{
{Text: "Male", Value: SexMale},
{Text: "Female", Value: SexFemale},
{Text: "Hermaphrodite", Value: SexHermaphrodite},
},
},
Label{
Text: "Weight:",
},
NumberEdit{
Value: Bind("Weight", Range{0.01, 9999.99}),
Suffix: " kg",
Decimals: 2,
},
Label{
Text: "Preferred Food:",
},
ComboBox{
Editable: true,
Value: Bind("PreferredFood"),
Model: []string{"Fruit", "Grass", "Fish", "Meat"},
},
Label{
Text: "Domesticated:",
},
CheckBox{
Checked: Bind("Domesticated"),
},
VSpacer{
ColumnSpan: 2,
Size: 8,
},
Label{
ColumnSpan: 2,
Text: "Remarks:",
},
TextEdit{
ColumnSpan: 2,
MinSize: Size{100, 50},
Text: Bind("Remarks"),
},
Label{
ColumnSpan: 2,
Text: "Patience:",
},
LineEdit{
ColumnSpan: 2,
Text: Bind("PatienceField"),
},
},
},
Composite{
Layout: HBox{},
Children: []Widget{
HSpacer{},
PushButton{
AssignTo: &acceptPB,
Text: "OK",
OnClicked: func() {
if err := db.Submit(); err != nil {
log.Print(err)
return
}
dlg.Accept()
},
},
PushButton{
AssignTo: &cancelPB,
Text: "Cancel",
OnClicked: func() { dlg.Cancel() },
},
},
},
},
}.Run(owner)
}
更多推荐
已为社区贡献7条内容
所有评论(0)