繁体   English   中英

自定义usercontrol textbox.text

[英]Custom usercontrol textbox.text

我试图做一个新的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"/>

向您的UserControlSelectAllTbox类)添加一个属性,该属性获取/设置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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM