我在c#wpf中创建了一个UserControl
。我想获得TextBox(txtCode)Property Box的所有属性
<UserControl x:Class="WpfApplication2.UserControl1"
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"
mc:Ignorable="d" Height="315.358" Width="758">
<Grid >
<TextBox x:Name="txtCode" HorizontalAlignment="Left" Height="97" Margin="134,62,0,0" TextWrapping="Wrap" Text="00001" VerticalAlignment="Top" Width="588" FontSize="30"/>
<Label Content="Code" HorizontalAlignment="Left" Margin="27,72,0,0" VerticalAlignment="Top" Width="138" Height="97" FontSize="30"/>
<Button Content="Submit" HorizontalAlignment="Left" Margin="290,186,0,0" VerticalAlignment="Top" Width="247" Height="48" FontSize="30"/>
</Grid>
</UserControl>
在Windows窗体中,如下面的图像
c#代码
public TextBox txtCodeProperties
{
get { return txtCode; }
set { txtCode = value; }
}
答案 0 :(得分:0)
如果要公开整个TextBox及其所有属性,则应查看x:FieldModifier。如果您只需要公开一些属性,则可以将它们公开为DependencyProperties,as shown here。
如何使用FieldModifier:
在UserControl1
中,将x:FieldModifier="public"
属性添加到TextBox:
<TextBox x:Name="txtCode" x:FieldModifier="public" HorizontalAlignment="Left" Height="97" Margin="134,62,0,0" TextWrapping="Wrap" Text="00001" VerticalAlignment="Top" Width="588" FontSize="30"/>
然后,无论何时使用UserControl1
,您现在都可以公开访问文本框。
示例用法(xaml):
<local:UserControl1 x:Name="myUserControl1" />
代码隐藏:
myUserControl1.txtCode.Text = "Hello, World!";