Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8445135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:34:59+00:00 2026-06-10T09:34:59+00:00

I want to create a custom control(or user control) which has both a textblock

  • 0

I want to create a custom control(or user control) which has both a textblock and a textbox as in the fig below:

enter image description here

Both the textblock and Textbox text properties are to be bound to the database fields and be able to apply styling etc. Which is the best approach for the same?

I have come up with a solution as below:

XAML for user control:

<UserControl x:Class="TestDependency.TextBlox"
         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"
         x:Name="abc"
         DataContext="{Binding RelativeSource={RelativeSource Self}}" >
<Grid Width="170">
    <TextBlock Height="23" HorizontalAlignment="Left" Margin="0,1,0,0" 
               Text="{Binding Path=Caption}"
               VerticalAlignment="Top" Width="52" />
    <TextBox Height="25" HorizontalAlignment="Left" Margin="53,-2,0,0" 
             x:Name="TextBox1"
             Text="{Binding Path=Value}"
             VerticalAlignment="Top" Width="120" />
</Grid>

Code behind for Usercontrol:

public  partial class TextBlox : UserControl
{
    public  TextBlox()
    {
        InitializeComponent();
    }

    public static DependencyProperty CaptionProperty = DependencyProperty.Register("Caption", typeof(string), typeof(TextBlox));
    public static DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(TextBlox));


    public string Caption
    {
        get
        {
            return (string)GetValue(CaptionProperty);
        }
        set
        {
            SetValue(CaptionProperty, value);
        }
    }


    public string Value
    {
        get
        {
            return (string)GetValue(ValueProperty);
        }
        set
        {
            SetValue(ValueProperty, value);
        }
    }

    static void ValueChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args)
    {
        TextBlox inst = (TextBlox)property;
        inst.TextBox1.Text = (string)args.NewValue;
    }
}

XAML for actual form where Usercontrol is used:

 <Grid x:Name="grdmain">
    <my:TextBlox Caption="{Binding XValue, Mode=TwoWay}" Value="{Binding WindowName, Mode=TwoWay}" 
        HorizontalAlignment="Left" Margin="246,197,0,0"  x:Name="textBlox1" VerticalAlignment="Top" />
</Grid>

Code behind:

 public partial class MainWindow : Window
{
    DataEntities dt = new DataEntities();
    CoOrdinate oCord;
    public MainWindow()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        oCord = dt.CoOrdinates.First();
        grdmain.DataContext = oCord;
    }
}

Still the binding doesnt work. But the code :

textBlox1.Caption = "test"; 

is working fine. where am I wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:35:01+00:00Added an answer on June 10, 2026 at 9:35 am

    Create an user control with a textblock and textbox and bind the values to that control.

    <YourUserControl TextBlockText="{Binding TextBlockText}" TextBoxText="{Binding TextBoxText, Mode=TwoWay}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want a create a custom/user control that has children. For Example, I want
I am trying to create a custom user control in WPF. I want to
I want to create a new custom control derived from ContentControl (it will be
i want to create the custom menu control in UIWebView, by using - (void)
I want to create WatermarkTextBox in WP7.1 I followed the link http://www.windowsphonegeek.com/articles/WP7-WatermarkedTextBox-custom-control But that
Which is the right MVC way to create custom form control (like custom drop
I have a Windows form which hosts a custom User Control. This User Control
I want to create a custom control derived from TPanel that contains an image
Currently if I want to test a user control, I add my custom tags
I have a custom user control in XAML that creates a ItemsControl which ItemsPanel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.