自定义用户控件文本框文本



我正在尝试创建一个新UserControl(TextBox选择焦点上的所有文本(,但我无法从代码中更新 .text。

我想要实现的是:

kolicinaTbox.text = "test"

我的自定义控件是:

<UserControl x:Class="SelectAllTbox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Demo"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="0"  VerticalAlignment="Top" Width="120"/>
</Grid>

Public Class SelectAllTbox
Private Sub textBox_GotFocus(sender As Object, e As RoutedEventArgs) Handles textBox.GotFocus
    textBox.SelectAll()
End Sub
End Class    

这是我代码中的控件:

<demo:SelectAllTbox x:Name="kolicinaTbox" HorizontalAlignment="Left" Margin="82,152,0,0" VerticalAlignment="Top" Width="63"/>

UserControl(SelectAllTbox类(添加一个属性,该属性获取/设置TextBoxText 属性:

Public Property Text() As String
    Get
        Return textBox.Text
    End Get
    Set(ByVal value As String)
        textBox.Text = value
    End Set
End Property

然后,可以使用此属性设置TextBoxText 属性:

kolicinaTbox.Text = "test"

最新更新