0

以下のコードは、データバインドされていないテキストボックスのみを無効にします。

Private Sub DisableFields()
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If TypeOf (ctrl) Is TextBox Then
            CType(ctrl, TextBox).Enabled = False
        End If
    Next
End Sub
4

1 に答える 1

0

これは正しい方法ではありません。
各 TextBox の xaml に移動し、IsEnabled をビュー モデル/コード ビハインドに配置するプロパティ (expl : AllowEdit) にバインドする必要があります。このプロパティは NotifyPropertyChanged を発生させる必要があります。スタイルまたはデフォルトのスタイルでバインディングを設定することにより、TextBox のデフォルトの動作として設定することもできます。ここでオーバーライドする必要がある場合は、IsEnabled="true" を設定できます。

EDIT : 使用可能なすべての TextBox をループするには、VisualTreeHelper を使用してビジュアル ツリーをたどる必要があります。

  Public Sub ApplyOnAllDescendant(ByVal BaseUIElement As UIElement, 
                                      ByVal TargetType As Type, 
                                             ByVal NewIsEnabled As Boolean)
    If BaseUIElement Is Nothing Then Return
    If BaseUIElement.GetType() = TargetType Then
        BaseUIElement.IsEnabled = NewIsEnabled
    Else
        Dim ChildCount As Integer = VisualTreeHelper.GetChildrenCount(BaseUIElement)
        For i = 0 To ChildCount - 1
            ApplyOnAllDescendant(VisualTreeHelper.GetChild(BaseUIElement, i), 
                                           TargetType, NewIsEnabled)
        Next
    End If
  End Sub

あなたが呼び出すこと:

      ApplyOnAllDescendant(Me, GetType(TextBox), False)

バインディングがあるかどうかを知りたい場合は、使用できます

Dim Bindings = CType(BaseUIElement, Control)
                                 .GetBindingExpression(TextBox.TextProperty)

次に、Bindings の内容を確認します。

しかし、繰り返しますが、これは「クリーンな」方法ではありません。

于 2012-09-03T08:59:09.047 に答える