使用 FSharp.ViewModule 启用对话框确定按钮
Enabling dialog OK button with FSharp.ViewModule
我有一个简单的对话框,其中有两个用于输入人名和地址的字段,我只希望在它们都有值时启用“确定”按钮。以下是我观点的相关部分:
<TextBox Text="{Binding Name}" ... />
<TextBox Text="{Binding Address}" ... />
<Button Content="OK" IsEnabled="{Binding OK, Mode=OneWay}" ... />
这是我的视图模型:
namespace ViewModels
open FSharp.ViewModule
open FSharp.ViewModule.Validation
open FsXaml
type DialogView = XAML<"Dialog.xaml">
type DialogVM() as self =
inherit ViewModelBase()
let name = self.Factory.Backing( <@ self.Name @>, "", notNullOrWhitespace )
let address = self.Factory.Backing( <@ self.Address @>, "" )
let hasValue str = not( System.String.IsNullOrWhiteSpace( str ))
member x.Name
with get() = name.Value
and set value = name.Value <- value ; self.RaisePropertyChanged( <@ self.OK @> )
member x.Address
with get() = address.Value
and set value = address.Value <- value ; self.RaisePropertyChanged( <@ self.OK @> )
member x.OK with get() = hasValue x.Name && hasValue x.Address
但是“确定”按钮始终处于启用状态。我做错了什么?
问题是 TextBox.Text
= LostFocus
的默认绑定模式。
属性 只是没有更新。
所以:
<TextBox Text="{Binding Name,UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Text="{Binding Address,UpdateSourceTrigger=PropertyChanged}" />
但是 FSharp.ViewModule 中有一些内容可以让您的生活更轻松。在你的情况下是 属性 IsValid
:
let name = self.Factory.Backing( <@ self.Name @>, "", notNullOrWhitespace)
let address = self.Factory.Backing( <@ self.Address @>, "", notNullOrWhitespace)
member x.Name
with get() = name.Value
and set value = name.Value <- value
member x.Address
with get() = address.Value
and set value = address.Value <- value
在XAML中:
<Button Content="OK" IsEnabled="{Binding IsValid}" />
我有一个简单的对话框,其中有两个用于输入人名和地址的字段,我只希望在它们都有值时启用“确定”按钮。以下是我观点的相关部分:
<TextBox Text="{Binding Name}" ... />
<TextBox Text="{Binding Address}" ... />
<Button Content="OK" IsEnabled="{Binding OK, Mode=OneWay}" ... />
这是我的视图模型:
namespace ViewModels
open FSharp.ViewModule
open FSharp.ViewModule.Validation
open FsXaml
type DialogView = XAML<"Dialog.xaml">
type DialogVM() as self =
inherit ViewModelBase()
let name = self.Factory.Backing( <@ self.Name @>, "", notNullOrWhitespace )
let address = self.Factory.Backing( <@ self.Address @>, "" )
let hasValue str = not( System.String.IsNullOrWhiteSpace( str ))
member x.Name
with get() = name.Value
and set value = name.Value <- value ; self.RaisePropertyChanged( <@ self.OK @> )
member x.Address
with get() = address.Value
and set value = address.Value <- value ; self.RaisePropertyChanged( <@ self.OK @> )
member x.OK with get() = hasValue x.Name && hasValue x.Address
但是“确定”按钮始终处于启用状态。我做错了什么?
问题是 TextBox.Text
= LostFocus
的默认绑定模式。
属性 只是没有更新。
所以:
<TextBox Text="{Binding Name,UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Text="{Binding Address,UpdateSourceTrigger=PropertyChanged}" />
但是 FSharp.ViewModule 中有一些内容可以让您的生活更轻松。在你的情况下是 属性 IsValid
:
let name = self.Factory.Backing( <@ self.Name @>, "", notNullOrWhitespace)
let address = self.Factory.Backing( <@ self.Address @>, "", notNullOrWhitespace)
member x.Name
with get() = name.Value
and set value = name.Value <- value
member x.Address
with get() = address.Value
and set value = address.Value <- value
在XAML中:
<Button Content="OK" IsEnabled="{Binding IsValid}" />