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 9214225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:55:46+00:00 2026-06-18T01:55:46+00:00

I have some problems with a WPF custom control, I’m trying to make it

  • 0

I have some problems with a WPF custom control, I’m trying to make it work but just don’t get it:

Here is my problem, I’m creating a simple custom control that’s almost the same to a TextBox. This control has a dependency property named “Texto”, and the binding between the XAML and back-code of the custom control works fine, here is the code:

<UserControl x:Class="WpfCustomControlLibrary1.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" 
         d:DesignHeight="47" d:DesignWidth="147">
<Grid Height="43" Width="142">
    <TextBox Height="23" HorizontalAlignment="Left" Margin="12,8,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding Texto}"/>        
</Grid>

And the dependency property code:

public static readonly DependencyProperty TextoProperty = DependencyProperty.Register("Texto", typeof(string), typeof(UserControl1));

    public string Texto
    {
        get
        {
            return (string)GetValue(TextoProperty);
        }

        set
        {
            SetValue(TextoProperty, value);
        }
    }

Ok, now the problem: When I use this control in other windows I try to bind the “Texto” property to a viewmodel (as simple as everything else) but the property on the view model just dont change:

The ViewModel code:

 public class ViewModelTest
{
    public string SomeText { get; set; }
}

And the code of the applicatoin Window:

public ViewModelTest test;

    public Window1()
    {
        InitializeComponent();
    }        

    private void button1_Click_1(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(test.SomeText);
        MessageBox.Show(uc.Texto);
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        test = new ViewModelTest();
        this.DataContext = test;
    }

And the binding with the property of the view model:

<my:UserControl1 HorizontalAlignment="Left" Margin="27,12,0,0" Name="uc" VerticalAlignment="Top" Texto="{Binding Path=SomeText,Mode=TwoWay}"/>

Just for make it clearer, if I write “Hello” in the custom control and then I push the “button1”, the first message shows nothing and the second message shows “Hello”.

As you can see I’m fairly new into this, so I hope some of you can help me. Thanks.

  • 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-18T01:55:47+00:00Added an answer on June 18, 2026 at 1:55 am

    Your binding Texto="{Binding SomeText}" works fine, the problem is the rebinding from your user control to the inner textbox. Remember binding will ALWAYS, if not modified, refere to the DataContext. But your DataContext doesn’t contain the property Texto. Your control has that, To refere to that you need something called TemplateBinding, but this only works when you are in a ControlTemplate. Which you aren’t so what is the solution?
    You can use a special form of binding, by changing the source from the DataContext to a control with a given name: First give your UserControl a name

    <UserControl x:Class="WpfCustomControlLibrary1.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" 
             x:Name="mainControl"
             d:DesignHeight="47" d:DesignWidth="147">
    

    and now change the binding to refere to the control, not the DataContext of the control anymore.

     <TextBox Text="{Binding ElementName=mainControl, Path=Texto}"/>
    

    Now your ViewModel binds to your user control and the content of the user control binds to the user controls Texto property.

    Also one minor thing, what you called custom control, is in fact a user control, custom controls are something else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use a custom control in a WPF app, and I have
I'm having some problems with data binding in WPF. Here's the scenario: I have
I am trying to seed some data using custom database initializers, but can't get
I got some problems in Datagrid WPF I have a datagrid, and I want
i have some problems with a Query seem IN dosen't work with Group_concat, that
I have some problems with Javascript. In fact, I'm just newbie in that script
I have some problems with header() function. It works and doesn't work at the
I have some problems with my first Open Graph custom action: I've created a
I'm writting a WPF program.And now i have some problems. There is a Window
Just looking for a list of some popular WPF/Silverlight/C# sites out there for problems/troubleshooting

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.