更改值WPF用户控件

时间:2017-01-18 20:30:37

标签: wpf vb.net binding

我正在创建UserControl,但我无法刷新值。

当我使用TextBox中的值启动应用程序时,它已被捕获。但是当我更改了值并触摸按钮时,没有捕获新值。

这是用户控件XAML:

 <TextBox x:Name="txtCedula" md:HintAssist.Hint="Cedula"  Text="{Binding txtCedula1}" Style="{StaticResource MaterialDesignFloatingHintTextBox}" Height="40" Margin="113.496,81.01,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="243.5" />

这是代码VB:

Partial Public Class Conductor
    Public Property txtCedula1 As String
        Get
            Return txtCedula.Text
        End Get
        Set(value As String)
            txtCedula.Text = value
        End Set
  End Property
End Class

我尝试捕捉我输入的值

Private Sub btnGuardar_Click(sender As Object, e As RoutedEventArgs) Handles  btnGuardar.Click
    validarCampos()
End Sub
Private Sub CaptureValue()
            Dim conductor As New Conductor

            If conductor.txtCedula1 = Nothing Then
                       MessageBox.Show("Ivalid", "SmartTruck", MessageBoxButton.OK, MessageBoxImage.Information)

            End If
    End Sub

即使输入值

,我也总是收到无效消息

2 个答案:

答案 0 :(得分:1)

您正在创建UserControl的新实例:

Host

您应该访问它的现有实例。如果你已经在窗口的XAML标记中声明了UserControl元素,你应该给它一个x:Name:

Dim conductor As New Conductor

然后您可以使用此名称访问它:

<local:Conductor x:Name="uc" />

答案 1 :(得分:0)

您只需要实现INotifyPropertyChanged并使用它来装饰您的属性。否则,如果你有约束力 XAML:

__with__

它知道它可以设置一次,但不能设置任何更新:

如果您正在使用MVVM,这样的事情应该会有效。您还需要确保您的ViewModel或其他任何类都绑定到datacontext,它听起来就像是第一次显示它。

对于ViewModel来说是这样的:

 <Textbox Text = "{Binding Test}"' />
相关问题