0

I need to know in code whether a regular (non DP) property was directly set for this element in Xaml or in code.

For example, i have a DockPanel with multiple TextBox elements inside, for some elements i set the Width property to some value and for some i don't.

In code i want to iterate all children's of the DockPanel and detect whether the Width property was set directly set on this property, or just it becomes value from the parent (i.e. ActualWidth).

In case of Dependency Properties the answer is very clear, just use ReadLocalValue and check if it returns DependencyProperty.UnsetValue. this answer.

But this Function requires a Dependency property, i need this for regular properties like Width which is a Double.

Any advice?

Community
  • 1
  • 1
IFink
  • 724
  • 16
  • 28
  • 1
    to know if default value is used, compare with default: `width == 0` or `width == default(double)`. would you care to explain what is `inherited width` in case of regular (non DP) property? – ASh Oct 31 '16 at 07:01
  • 2
    Besides that `Width` is a dependency property (inherited from FrameworkElement), the question doesn't make sense. You can't be sure that a property was set in XAML. It might as well have been set in code behind. – Clemens Oct 31 '16 at 07:36
  • @Clemens and Ash, please see my edited question. Thanks in advance. – IFink Nov 01 '16 at 18:17
  • As already said, `Width` is a dependency property. It's default value is `double.NaN`, so it's even easier to check if a value was set, just by checking `!double.IsNaN(element.Width)` – Clemens Nov 01 '16 at 18:29
  • @Clemens, OK, let say that Width is a DP, but what about other properties that are not DPs? how can we check it. – IFink Nov 02 '16 at 07:15
  • Not at all of course. At least not without setting some flag in the property setter. – Clemens Nov 02 '16 at 08:08

0 Answers0