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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:21:59+00:00 2026-05-27T17:21:59+00:00

We are trying to get what the value of a DependencyProperty would be if

  • 0

We are trying to get what the value of a DependencyProperty would be if it weren’t locally set in the XAML. For instance, take this XAML…

<StackPanel Orientation="Vertical"
    TextElement.FontSize="24">

    <TextBlock Text="What size am I?"
        FontSize="{Binding FontSize, RelativeSource={RelativeSource Self}, Converter={StaticResource PercentageConverter}, ConverterParameter=0.75}" />

</StackPanel>

This of course doesn’t work. What I’ve done instead is this…

<TextBlock Text="What size am I?"
    FontSize="{Binding (TextElement.FontSize), RelativeSource={RelativeSource AncestorType=FrameworkElement}, Converter={StaticResource PercentageConverter}, ConverterParameter=0.5}" />

…which checks the parent’s value for the inherited FontSize property but that only works because FontSize does support inheritance. I’m looking for a solution for any DependencyProperty, not just ones that can be inherited.

Update

I changed the title of the question to be a little more generic as to what would be helpful to us. Specifically we’d love to be able to say ‘For this specific instance of a DependencyObject, what would the value of a DependencyProperty be for each of the precedence levels?’ When we query a DP, we of course get what the value after all precedence is applied. We’d love to say ‘What would it be one or two levels higher?’, etc.

  • 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-27T17:22:00+00:00Added an answer on May 27, 2026 at 5:22 pm

    A generic and robust solution should take into account the full rule set for DependencyProperty value precedence. I don’t think that it is safe to say that if a value is not set locally, then its value would be the default value – the value instead could then be provided by a style, a trigger, etc.

    I would suggest taking a look at the class DependencyPropertyHelper. This class contains a method called GetValueSource which when given a DependencyObject and a DependencyProperty will return a BaseValueSource result that tells you where the value for the property is coming from (style, style trigger, inherited, local, etc).

    That being said, I think it would not be too hard to write an attached behaviour which you could use in XAML to return the “non-local” value for a specified DependencyProperty. This behaviour would check if the value source is local, and if it is clone the element, add it to the logical tree, and clear the local value. It would then read the value from the specified DependencyProperty on the clone, and set a read-only attached DependencyProperty to this value. This way you are letting the WPF property engine do the work for you.

    Hope this helps!

    Edit:

    Here is a proof of concept for the attached behaviour. The behaviour has 2 properties:

    1. PropertyToInspectValue – The DependencyProperty who’s value you
      wish to inspect.
    2. InspectedValue – The computed non-local value for the specified DependencyProperty.

    In XAML you bind to these 2 properties (setting one, reading the other):

    <StackPanel>
        <StackPanel>
            <StackPanel.Resources>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="FontSize" Value="30" />
                </Style>
                <Style x:Key="NamedStyle" TargetType="{x:Type TextBlock}">
                    <Setter Property="FontSize" Value="35" />
                </Style>
            </StackPanel.Resources>
            <!-- Without local value - uses default style (30)-->
            <TextBlock l:DependencyPropertyValueHelper.PropertyToInspectValue="TextElement.FontSize"
                       Text="{Binding Path=(l:DependencyPropertyValueHelper.InspectedValue), RelativeSource={RelativeSource Self}}"
                       FontSize="15" />
            <!-- Without local value - uses named style (35)-->
            <TextBlock l:DependencyPropertyValueHelper.PropertyToInspectValue="TextElement.FontSize"
                       Style="{StaticResource NamedStyle}"
                       Text="{Binding Path=(l:DependencyPropertyValueHelper.InspectedValue), RelativeSource={RelativeSource Self}}"
                       FontSize="15" />
        </StackPanel>
        <StackPanel TextElement.FontSize="25">
            <!-- Without local value - uses inherited value (25) -->
            <TextBlock l:DependencyPropertyValueHelper.PropertyToInspectValue="TextElement.FontSize"
                       Text="{Binding Path=(l:DependencyPropertyValueHelper.InspectedValue), RelativeSource={RelativeSource Self}}"
                       FontSize="15" />
        </StackPanel>
        <!-- Without local value - uses default font size (11) -->
        <TextBlock l:DependencyPropertyValueHelper.PropertyToInspectValue="TextElement.FontSize"
                   Text="{Binding Path=(l:DependencyPropertyValueHelper.InspectedValue), RelativeSource={RelativeSource Self}}"
                   FontSize="15" />
    </StackPanel>
    

    The example above will correctly show the values: 30, 35, 25, and 11. A couple of limitations in the code below is that the DependencyObject which we are inspecting must be a child of a Panel, and that currently only the Style property is copied over, but really all properties should be cloned since triggers in the style could use any property to change the value a DependencyProperty. Food for thought I guess …

    Attached Behaviour:

    public static class DependencyPropertyValueHelper
    {
        private static bool _isUpdating;
    
        #region InspectedValue Read-only Attached Dependency Property
    
        public static object GetInspectedValue(DependencyObject d)
        {
            return d.GetValue(InspectedValueProperty);
        }
    
        private static readonly DependencyPropertyKey InspectedValuePropertyKey =
            DependencyProperty.RegisterAttachedReadOnly("InspectedValue", typeof (object),
                                                        typeof (DependencyPropertyValueHelper),
                                                        new FrameworkPropertyMetadata(null));
    
        public static readonly DependencyProperty InspectedValueProperty = InspectedValuePropertyKey.DependencyProperty;
    
        #endregion
    
        #region PropertyToInspect Attached Dependency Property
    
        public static void SetPropertyToInspectValue(DependencyObject d, DependencyProperty dependencyProperty)
        {
            d.SetValue(PropertyToInspectValueProperty, dependencyProperty);
        }
    
        public static DependencyProperty GetPropertyToInspectValue(DependencyObject d)
        {
            return (DependencyProperty)d.GetValue(PropertyToInspectValueProperty);
        }
    
        public static readonly DependencyProperty PropertyToInspectValueProperty =
            DependencyProperty.RegisterAttached("PropertyToInspectValue", typeof (DependencyProperty),
                                                typeof (DependencyPropertyValueHelper),
                                                new FrameworkPropertyMetadata(OnPropertyToInspectValuePropertyChanged));
    
        #endregion
    
        #region Private ValueChanged Attached Dependency Property
    
        public static readonly DependencyProperty ValueChangedProperty =
            DependencyProperty.RegisterAttached("ValueChanged", typeof(object),
                                                typeof(DependencyPropertyValueHelper),
                                                new FrameworkPropertyMetadata(OnValuePropertyChanged));
    
        #endregion
    
        private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!_isUpdating)
                DetermineNonLocalValue(d, GetPropertyToInspectValue(d));
        }
    
        private static void OnPropertyToInspectValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var dependencyProperty = (DependencyProperty) e.NewValue;
            DetermineNonLocalValue(d, dependencyProperty);
    
            var binding = new Binding
                              {
                                  RelativeSource = new RelativeSource(RelativeSourceMode.Self),
                                  Path = new PropertyPath(dependencyProperty.Name),
                              };
    
            BindingOperations.SetBinding(d, ValueChangedProperty, binding);
        }
    
        private static void DetermineNonLocalValue(DependencyObject d, DependencyProperty dependencyProperty)
        {
            var element = d as FrameworkElement;
    
            if (element == null)
                return;
    
            var valueSource = DependencyPropertyHelper.GetValueSource(element, dependencyProperty);
    
            if (valueSource.BaseValueSource == BaseValueSource.Local)
            {
                _isUpdating = true;
    
                var clonedDependencyObject = Activator.CreateInstance(element.GetType()) as FrameworkElement;
                var parent = VisualTreeHelper.GetParent(element) as Panel;
    
                // Currently only works if parent is a panel
                if (parent != null)
                {
                    // Copy any property which could impact the DP's value ...
                    // Probably check if this is a control and copy the ControlTemplate too ...
                    if (element.Style != null)
                        clonedDependencyObject.Style = element.Style;
    
                    parent.Children.Add(clonedDependencyObject);
    
                    // Let WPF provide us with the non-local value
                    element.SetValue(InspectedValuePropertyKey, clonedDependencyObject.GetValue(dependencyProperty));
    
                    parent.Children.Remove(clonedDependencyObject);
                }
    
                _isUpdating = false;
            }
            else
            {
                element.SetValue(InspectedValuePropertyKey, d.GetValue(dependencyProperty));
            }
        }
    }
    

    Edit 2

    To elaborate on this approach. There is no chain of values exposed by WPF which we can inspect (or at least that I know of). The code above attempts to discover what the value would be if it was not set locally (from whatever value source: inherited, styles, triggers, etc). When it discovers this value it then populates the InspectedValue property with the result, which then can be read from.

    A first attempt at this would be to:

    1. Clear the local value for the DP, store it away somewhere (e.g. using DependencyObject.ClearValue)
    2. Query the DP for its new value which we will assume is what the value would have been if it was not set locally. Record this value.
    3. Restore the original value from step 1.

    This approach fails – in that during step 2 your property will not pick up values from default styles (for example) since the style has already been applied, and simply removing a local value from a random DependencyPropery does not trigger a re-application.

    Another approach would be in step 2 above – remove the element from the logical tree, and then add it back (since when elements are added to the tree, WPF goes through all possible value sources to determine the value of each DP). This too fails for the same reason – you are not guaranteed that styles will be re-applied.

    The attached behaviour above tries a third approach – clone an element but make sure the the property in question is not set on the clone, and then add it to the logical tree. At this point WPF will then process the element as it would have with the original element and apply a value to the DP (inherited, triggers, style, etc). We can then read its value, and store it away. As demonstrated, this approach works for common use-cases, but is not perfect since if the non-local value was coming from a Trigger using a read-only property like IsMouseOver it would not pick it up (and a deep copy of the original object state would not fix this).

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

Sidebar

Related Questions

I am trying to get value of selected item from dropdown menu via val()
I am trying to get value from passwordbox for developing a simple login functionality,
While i am trying to get value from arraylist,i got the following errors.My sample
Trying to get a value out of 2d array inside of a hash and
I'm trying to get the value of an <input type=text> with the method keyup
I am trying to get the value of javascript pop-up after using file_get_contents .
I'm trying to get the value of a <select> when a button is clicked.
I am trying to get the value user selected from search prompt and pass
I'm trying to get the value of the Text property of an asp:label with
I'm trying to get a value from an HTML page that has the name

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.