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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:51:15+00:00 2026-06-14T13:51:15+00:00

So I’m playing around with some WPF UserControls and DependencyProperties. I’ve got the following

  • 0

So I’m playing around with some WPF UserControls and DependencyProperties.
I’ve got the following UserControl:

<UserControl x:Class="ValueInser.UserControls.FirmPersonInsert"
         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:self="clr-namespace:ValueInser.UserControls"
         x:Name="uc1"
         mc:Ignorable="d" 
         d:DesignHeight="225" d:DesignWidth="450"
         DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100px"/>
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="40"/>
        <RowDefinition IsEnabled="{Binding ElementName=rbtnPers, Path=IsChecked}"/>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <GroupBox Grid.Column="1" Header="Privat-/Juristische Person" Width="150" HorizontalAlignment="Left">
        <StackPanel Orientation="Horizontal">
            <RadioButton Content="Person" IsChecked="{Binding Path=IsCompany}"/>
            <Line Height="1" Width="25" />
            <RadioButton Content="Firma" Name="rbtnCompany" />
        </StackPanel>
    </GroupBox>

    <Label Content="Firma" Grid.Column="0" Grid.Row="1" />
    <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Path=Company}" IsEnabled="{Binding ElementName=rbtnCompany, Path=IsChecked}"/>

    <Label Content="Anrede" Grid.Column="0" Grid.Row="2" />
    <TextBox Grid.Column="1" Grid.Row="2" Text="{Binding Path=Salutation}" />

    <Label Content="Name" Grid.Column="0" Grid.Row="3" />
    <TextBox Grid.Column="1" Grid.Row="3" Text="{Binding Path=LastName, RelativeSource={RelativeSource self}}" />

    <Label Content="Vorname" Grid.Column="0" Grid.Row="4" />
    <TextBox Grid.Column="1" Grid.Row="4" Text="{Binding Path=FirstName}"/>

    <Label Content="Strasse" Grid.Column="0" Grid.Row="5" />
    <TextBox Grid.Column="1" Grid.Row="5" Text="{Binding Path=Street}" />

    <Label Content="Ort" Grid.Column="0" Grid.Row="6" />
    <TextBox Grid.Column="1" Grid.Row="6" Text="{Binding Path=City}" />

    <Label Content="PLZ" Grid.Column="0" Grid.Row="7" />
    <TextBox Grid.Column="1" Grid.Row="7" Text="{Binding Path=ZIP}" />
</Grid>

With this Code Behind:

        #region Dependency Properties
    public static readonly DependencyProperty _company = DependencyProperty.Register("Company", typeof(string), typeof(UserControl));
    public string Company
    {
        get
        {
            return this.GetValue(_company) as string;
        }
        set
        {
            this.SetValue(_company, value);
        }
    }

    public static readonly DependencyProperty _salutation = DependencyProperty.Register("Salutation", typeof(string), typeof(UserControl));
    public string Salutation
    {
        get
        {
            return this.GetValue(_salutation) as string;
        }
        set
        {
            this.SetValue(_salutation, value);
        }
    }

    public static readonly DependencyProperty _lastName = DependencyProperty.Register("LastName", typeof(string), typeof(UserControl));
    public string LastName
    {
        get
        {
            return this.GetValue(_lastName) as string;
        }
        set
        {
            this.SetValue(_lastName, value);
        }
    }

    public static readonly DependencyProperty _firstName = DependencyProperty.Register("FirstName", typeof(string), typeof(UserControl));
    public string FirstName
    {
        get
        {
            return this.GetValue(_firstName) as string;
        }
        set
        {
            this.SetValue(_firstName, value);
        }
    }

    public static readonly DependencyProperty _street = DependencyProperty.Register("Street", typeof(string), typeof(UserControl));
    public string Street
    {
        get
        {
            return this.GetValue(_street) as string;
        }
        set
        {
            this.SetValue(_street, value);
        }
    }

    public static readonly DependencyProperty _city = DependencyProperty.Register("City", typeof(string), typeof(UserControl));
    public string City
    {
        get
        {
            return this.GetValue(_city) as string;
        }
        set
        {
            this.SetValue(_city, value);
        }
    }

    public static readonly DependencyProperty _zip = DependencyProperty.Register("ZIP", typeof(string), typeof(UserControl));
    public string ZIP
    {
        get
        {
            return this.GetValue(_zip) as string;
        }
        set
        {
            this.SetValue(_zip, value);
        }
    }

    public static readonly DependencyProperty _isCompany = DependencyProperty.Register("IsCompany", typeof(bool), typeof(UserControl));
    public bool IsCompany
    {
        get
        {
            return !(bool)this.GetValue(_isCompany);
        }
        set
        {
            this.SetValue(_isCompany, value);
        }
    }
    #endregion

And now the tricky part comes to play.
So that I can bind the textboxs text property to the dependencyproperties in the code behind I have to set the datacontext to itself.

When I want to use this control now on a Window I have a problem:

      <uc:FirmPersonInsert DataContext="{DynamicResource ResourceKey=mvm}"
       IsCompany="{Binding Path=ProjectArchitect.IsCompany}"
       Company="{Binding Path=ProjectArchitect.Company}" 
       LastName="{Binding Path=ProjectArchitect.LastName}"
       FirstName="{Binding Path=ProjectArchitect.FirstName}"
       Street="{Binding Path=ProjectArchitect.Street}"
       City="{Binding Path=ProjectArchitect.City}"
       ZIP="{Binding Path=ProjectArchitect.ZIP}"/>

This Properties are stored on the ViewModel of the Application. But now the whole construct starts to get weired.
I can’t change the datacontext because otherwise the bindings on the usercontrol won’t work anymore.

I think I missunderstand something completly how all of that should work.

I need the dependencyprops so that a user of my control can simply bind to the new “Property Names” like LastName.

Please can someone explain me, where am I thinking 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-14T13:51:16+00:00Added an answer on June 14, 2026 at 1:51 pm

    Remove the DataContext="{Binding RelativeSource={RelativeSource Self}}"> from the usercontrol and instead:

    <Grid DataContext="{Binding ElementName=uc1}">.

    Also remove all the RelativeSources from the individual textboxes.

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string

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.