Bind Button.IsEnabled to custom validation with XAML?
I am sorry I didn't know how to title my question any better, you name it if you got a good 1.
I have an entity Contact
.
this per开发者_如何学Cson has navigation properties: Address
, Phones
(A collection of Phone
).
They all implement a custom interface that exposes a property IsValid
.
In the contact edito form I have an OK button, I want its IsEnabled
property to be true only if:
Contact.IsValid
Contact.Address.IsValid
Array.TrueForAll(Person.Phones.Cast(Of Phone).ToArray, Function(p) p.IsValid)
I prefer not to use a converter, but to do it Xamly only, anyway, I don't mind to use local code (i.e. reference to a method in the current page that returns a boolean value like System.Web.UI.WebControls.CustomValidator or something like this), but I really don not want a converter unless it's the very only option.
Thru this I was able to solve my question.
I bound the IsEnabled property to a dependency-property I placed in the code-behind that returns if items are valid which I called 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
I set the IsValid property at the OnPropertyChanged event handlers of ALL the items to be validated.
In the Xaml I set the button like this:
<Button Name="btnOk" IsEnabled="{Binding IsValid}"/>
精彩评论