简体   繁体   English

将属性附加到usercontrol并在设计时更新它

[英]Attach property to usercontrol and updating it at design time

How can I create a user control like the Textbox? 如何创建像Textbox这样的用户控件? For example when I change the Text property of a Textbox control the new text appears on the window that I am currently working with. 例如,当我更改Textbox控件的Text属性时,新文本将出现在我当前使用的窗口中。

In my project I have a lot of places where the user has to enter information therefore I want to create a InputField user control. 在我的项目中,我有很多用户必须输入信息的地方,因此我想创建一个InputField用户控件。 (that usercontrol consists of a label an a textbox with custom style) (该usercontrol由标签和具有自定义样式的文本框组成)

Here is the xaml for my user control: 这是我的用户控件的xaml:

<UserControl x:Class="PDV.UserControls.InputField"
         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" >
   <Grid>
      <StackPanel>
          <Label Content="{Binding Path=LblContent}" HorizontalAlignment="Left" VerticalAlignment="Top"  />
          <TextBox Height="23" Margin="5,-5,2,2" Name="textBox1" VerticalAlignment="Top" />
      </StackPanel>
   </Grid>
</UserControl>

and the code behind for that user control: 以及该用户控件的代码:

namespace PDV.UserControls
{
    public partial class InputField : UserControl
    {
        public static DependencyProperty MessageProperty = DependencyProperty.Register(
            "LblContent", typeof(string), typeof(UserControl));

        public string LblContent{
            get{
                return (string)GetValue(MessageProperty);
            }
            set{
                SetValue(MessageProperty, value);
            }
        }

        //Constructor
        public InputField(){
            InitializeComponent();
            this.DataContext = this;
        }
    }
}

so on my main window I will be able to use that user control by: 所以在我的主窗口上,我将能够通过以下方式使用该用户控件:

1) importing the namespace where that user control is: 1)导入该用户控件所在的命名空间:

  xmlns:myCtrl ="clr-namespace:PDV.UserControls"

2) placing that control in that window: 2)将该控件放在该窗口中:

  <myCtrl:InputField LblContent="hello" Margin="0,0,483,0" Height="49" VerticalAlignment="Top"></myCtrl:InputField>

What do I have to do so that when I update LblContent="hello" it renders on the window? 当我更新LblContent="hello"时,我需要做什么才能在窗口上渲染? It will be nice for it to render at design time not just at runtime 它不仅仅是在运行时在设计时渲染它会很好

I think that the second type of might be InputField public static DependencyProperty MessageProperty = DependencyProperty.Register( "LblContent", typeof(string), typeof(InputField)); 我认为第二种类型可能是InputField public static DependencyProperty MessageProperty = DependencyProperty.Register(“LblContent”,typeof(string),typeof(InputField));

I never try to set the DataContext in your way, eventually try to give a name at the usercontrol x:Name="Root" then change the binding like this: Content="{Binding Path=LblContent, ElementName=Root}" 我从不尝试以你的方式设置DataContext,最终尝试在usercontrol x上给出一个名称:Name =“Root”然后更改绑定,如下所示:Content =“{Binding Path = LblContent,ElementName = Root}”

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

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