3

我有一个简单的对话框,其中有两个字段用于输入一个人的姓名和地址,我只希望在它们都有值时启用 OK 按钮。以下是我观点的相关部分:

<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

但“确定”按钮始终处于启用状态。我究竟做错了什么?

4

1 回答 1

3

问题在于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}"  />
于 2016-12-02T10:49:42.797 回答