2

对不起,我不知道如何更好地为我的问题命名,如果你得到一个好的 1,你就给它命名。

我有一个实体Contact。此人具有导航属性:Address, Phones( 的集合Phone)。

它们都实现了一个公开属性的自定义接口IsValid

在联系人编辑表单中,我有一个确定按钮,我希望它的IsEnabled属性只有在以下情况下才为真:

Contact.IsValid
Contact.Address.IsValid
Array.TrueForAll(Person.Phones.Cast(Of Phone).ToArray, Function(p) p.IsValid)

我不喜欢使用转换器,而是只使用 Xamly,无论如何,我不介意使用本地代码(即引用当前页面中返回布尔值的方法,如System.Web.UI.WebControls。 CustomValidator或类似的东西),但我真的不想要转换器,除非它是唯一的选择。

4

1 回答 1

0

通过这个,我能够解决我的问题。

我将 IsEnabled 属性绑定到我放置在代码隐藏中的依赖属性,如果项目有效则返回,我称之为 IsValid:

Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
    btnOK.DataContext = Me
End Sub

Private m_IsValid As Boolean
Public Property IsValid() As Boolean
    Get
        Return GetValue(IsValidProperty)
    End Get
    Private Set(ByVal value As Boolean)
        SetValue(IsValidPropertyKey, value)
    End Set
End Property

Private Shared ReadOnly IsValidPropertyKey As DependencyPropertyKey = _
                        DependencyProperty.RegisterReadOnly("IsValid", _
                        GetType(Boolean), GetType(PersonEditor), _
                        New FrameworkPropertyMetadata(False))

Public Shared ReadOnly IsValidProperty As DependencyProperty = _
                       IsValidPropertyKey.DependencyProperty

我在所有要验证的项目的 OnPropertyChanged 事件处理程序中设置 IsValid 属性。

在 Xaml 中,我将按钮设置为:

<Button Name="btnOk" IsEnabled="{Binding IsValid}"/>
于 2009-11-14T18:05:39.237 回答