简体   繁体   English

如何获取usercontrol的TextBox内部的所有属性

[英]How to get all Properties of inside of usercontrol`s TextBox

I created a UserControl in c# wpf. 我在c#wpf中创建了一个UserControl I want to get all properties of TextBox (txtCode) Property Box 我想获取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>

In Windows form something Like below Image 在Windows中,如下图所示

在此处输入图片说明

c# Code C#代码

  public TextBox txtCodeProperties { get { return txtCode; } set { txtCode = value; } } 

If you want to expose the whole TextBox and all of it's properties, you should look into x:FieldModifier . 如果要公开整个TextBox及其所有属性,则应查看x:FieldModifier If you only need to expose some of the properties, you could expose them as DependencyProperties, as shown here . 如果只需要公开某些属性,则可以将它们公开为DependencyProperties, 如下所示

How to use the FieldModifier: 如何使用FieldModifier:

In your UserControl1 , add the x:FieldModifier="public" attribute to your TextBox: 在您的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"/>

Then, whenever you use UserControl1 , you now have public access to the textbox. 然后,无论何时使用UserControl1 ,您现在都可以对文本框进行公共访问。

Example usage (xaml): 用法示例(xaml):

<local:UserControl1 x:Name="myUserControl1" />

Code-behind: 后台代码:

myUserControl1.txtCode.Text = "Hello, World!";

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

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