自定义usercontrol textbox.text

时间:2017-06-22 08:44:30

标签: wpf vb.net

我正在尝试制作一个新的UserControlTextBox选择焦点上的所有文字),但我无法从代码中更新.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"/>

1 个答案:

答案 0 :(得分:1)

将属性添加到UserControlSelectAllTbox类),获取/设置Text的{​​{1}}属性:

TextBox

然后,您可以使用此属性设置Public Property Text() As String Get Return textBox.Text End Get Set(ByVal value As String) textBox.Text = value End Set End Property 的{​​{1}}属性:

Text