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

  • Home
  • SEARCH
  • 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 3242348
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:18:18+00:00 2026-05-17T18:18:18+00:00

I have been reading on Dependency properties for a few days and understand how

  • 0

I have been reading on Dependency properties for a few days and understand how they retrieve the value rather than to set/get them as in CLR properties. Feel free to correct me if I am wrong.

From my understanding all WPF controls like a TextBlock, Button etc that derive from DependencyObject would also contain dependency properties to store their values, instead of using CLR properties. This has the advantage of overriding local values in case animations are used, or inherit values if no local value is set at all etc.

I am now trying to come up with some samples to create and use my own dp.

1) Is it possible to create my own dependency property on an existing WPF control? Let say I would like a dependency property of type integer on WPF Textblock class ? Or do i have to create a new class derived from TextBlockBase in order to create my dependency property above in there?

2) In either case, let say I have created a dependency property on a WPF textblock class. Now I would like to utilize it by binding the content of label to that dependency property of the TextBlock. So that the label would always show the actual value of TextBlock’s dp, no matter if its inherited or set locally.

Hopefully someone can help me with these two examples…
Many Thanks,
Kave

  • 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-17T18:18:18+00:00Added an answer on May 17, 2026 at 6:18 pm

    You can use attached properties for it.

    Define your property MyInt:

    
    namespace WpfApplication5
    {
        public class MyProperties
        {
            public static readonly System.Windows.DependencyProperty MyIntProperty;
    
            static MyProperties()
            {
                MyIntProperty = System.Windows.DependencyProperty.RegisterAttached(
                    "MyInt", typeof(int), typeof(MyProperties));
            }
    
            public static void SetMyInt(System.Windows.UIElement element, int value)
            {
                element.SetValue(MyIntProperty, value);
            }
    
            public static int GetMyInt(System.Windows.UIElement element)
            {
                return (int)element.GetValue(MyIntProperty);
            }
        }
    }
    
    

    Bind label content:

    
    <Window x:Class="WpfApplication5.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication5"
        Title="Window1" Height="300" Width="300">
        <Grid>
            <Label Margin="98,115,51,119" Content="{Binding Path=(local:MyProperties.MyInt), RelativeSource={x:Static RelativeSource.Self}}" local:MyProperties.MyInt="42"/>
        </Grid>
    </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading about dependency properties in several books but all have one
I have been trying to set up OpenCV for the past few days with
I've been reading about dependency injection and have a simple question. I can understand
I have been reading up on Dependency Injection frameworks. I really fell in love
I have been reading for a couple of days about Unity in MVC, and
I have been reading around the definition of OOP and couldn't get why PHP
I have been reading through the C++ FAQ and was curious about the friend
I have been reading the MSDN documentation on subclassing and I have been successful
I have been reading about the differences between Table Variables and Temp Tables and
I have been reading the proper article in MSDN, Strong-Named Assemblies and a related

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.