gpt4 book ai didi

wpf - 使用 FSharp.ViewModule 启用对话框确定按钮

转载 作者:行者123 更新时间:2023-12-01 13:40:16 26 4
gpt4 key购买 nike

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

<TextBox Text="{Binding Name}" ... />
<TextBox Text="{Binding Address}" ... />
<Button Content="OK" IsEnabled="{Binding OK, Mode=OneWay}" ... />

这是我的 View 模型:

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 的默认绑定(bind)模式 = 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}"  />

关于wpf - 使用 FSharp.ViewModule 启用对话框确定按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40927769/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com