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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:26:03+00:00 2026-05-23T20:26:03+00:00

I have the following sample code in WPF. It is working well when I

  • 0

I have the following sample code in WPF. It is working well when I put the xaml and corresponding C# code inside windows, but when I am using a user control, the databinding doesn’t work. why?

Here is the code:

User control XAML:

<UserControl x:Class="TestWpf.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" 
          xmlns:ec="clr-namespace:TestWpf"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="MainGrid" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition  Height="*"/>
    </Grid.RowDefinitions>
    <WrapPanel HorizontalAlignment="Right"  VerticalAlignment="Stretch" Grid.Row="0" >
        <Button Content="Edit" HorizontalAlignment="Right"  Name="editButton1" VerticalAlignment="Stretch" Click="button1_Click" />
    </WrapPanel>
    <Grid x:Name="patientDataGrid" Margin="10,10,10,10" Grid.Row="1">
        <Grid.Resources>
            <Style TargetType="TextBox" >
                <Setter Property="IsEnabled" Value="{Binding Path=IsEditing, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ec:UserControl1}}}" />
            </Style>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />

        </Grid.ColumnDefinitions>
        <TextBox x:Name="textBox1"  Grid.Column="0" Text="!234" />
        <TextBox x:Name="textBox2"    Grid.Column="1" />
    </Grid>
</Grid>
</UserControl>

The user control C# code:

 public partial class UserControl1 : UserControl
 {
    public static readonly DependencyProperty IsEditingProperty = DependencyProperty.Register(
          "IsEditing", typeof(Boolean), typeof(MainWindow), new PropertyMetadata(false));

    public Boolean IsEditing
    {
        get { return (Boolean)GetValue(IsEditingProperty); }
        set { SetValue(IsEditingProperty, value); }
    }
    public UserControl1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        IsEditing = !IsEditing;
    }
}

MainWindows XAML:

<Window x:Class="TestWpf.MainWindow"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ec="clr-namespace:TestWpf"
    Title="MainWindow" Height="200" Width="525">

    <ec:UserControl1 x:Name="c" />

</Window>

I notice that if I move all C# and xaml code from user control to MainWindows, the binding is working without any problem.

What is wrong with the code?

  • 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-05-23T20:26:04+00:00Added an answer on May 23, 2026 at 8:26 pm

    You registered the dependency property to be owned by MainWindow instead of your UserControl.

    public static readonly DependencyProperty IsEditingProperty =
        DependencyProperty.Register
        (
            "IsEditing",
            typeof(Boolean),
            typeof(MainWindow), // <-- Here
            new PropertyMetadata(false)
        );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sample template code. {# @T('core.layout:Logged in as {0} {1}', @user('firstname'),
I have a simple 3D cube that I can rotate using the following code:
I have the following sample code where I download a pdf from the European
I have the following code class sample { public void update(List l) { l
I'll try and keep my sample code very straightforward, but it may have errors
In my prism module, I have the following code snippet: using (var manifestResourceStream =
I have the following sample code that zooms each time a button is pressed:
I have created a graphical image with the following sample code. BufferedImage bi =
I have a simple application with the following code: FileInfo[] files = (new DirectoryInfo(initialDirectory)).GetFiles();
I have the following sample XML structure: <SavingAccounts> <SavingAccount> <ServiceOnline>yes</ServiceOnline> <ServiceViaPhone>no</ServiceViaPhone> </SavingAccount> <SavingAccount> <ServiceOnline>no</ServiceOnline>

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.