3

How can I create a user control like the 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.

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. (that usercontrol consists of a label an a textbox with custom style)

Here is the xaml for my user control:

<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:

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

2) placing that control in that window:

  <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? It will be nice for it to render at design time not just at runtime

Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • You sure you need a custom control for a Label and TextBox. You can apply a style to your Label and TextBox. And the Label will render at design time. By the time you add the syntax to bind the TextBox you are not saving many keystrokes and in my view code that is harder to follow. – paparazzo Mar 21 '12 at 22:55
  • Yeah i was trying to figure that out. The label and the textox need to be inside a stackpanel. I was trying to figure out how to create a style for the stackpanel so that its children could have a specific style instead of having to create a style for the textbox the label and the stackpanel. Every time y needed a new input field I had to place three controls versus just draging a user control. – Tono Nam Mar 23 '12 at 07:37

1 Answers1

1

I think that the second type of might be 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}"

pluka
  • 133
  • 1
  • 5
  • Because I saw that you manage the datacontext in a different way, but I inserted the same comment in the new post. Yes I could edit it you're right – pluka Mar 22 '12 at 07:33