[英]Access from UserControl's properties to all of a inside control's properties
[英]How to get all Properties of inside of usercontrol`s TextBox
我在c#wpf中创建了一个UserControl
。 我想获取TextBox(txtCode)属性框的所有属性
<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; } }
如果要公开整个TextBox及其所有属性,则应查看x:FieldModifier 。 如果只需要公开某些属性,则可以将它们公开为DependencyProperties, 如下所示 。
如何使用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!";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.