Search code examples
c#bindingcastingopacitygraphic

Get Sending Control's Name property to be used in opacity change


Ok I'll try explain as best as possible.

I have a method, "HandleGraphic". This method will be..Handling the opacity of a given control via a Logarithmic function.

The method parameters are: (object SendingObject, float From, float To)

My question is: How do I cast/bind the SendingObject in such a way as to directly be able to manipulate it's values/properties?

For instance: Say the textbox calls the HandleGraphic method. The HandleGraphic needs the sending control's Name property "txtbox" to adjust it's Opacity property.

There must be a way of implementing a global function of casting or binding to directly access the calling object's property.

For instance: SendingObject.Opacity = X;

Any help would be much appreciated.


Solution

  • If you are talking about WPF (WinForms controls don't have an Opacity property), both the Name and Opacity properties are members of Control (specifically System.Windows.Controls.Control). Why not change the signature of HandleGraphic to (Control control, float from, float to)? All controls derive from Control, so you can pass any of them to that method.

    Correction

    The Name property is a member of FrameworkElement and the Opacity property is a member of UIElement. So to work with both Name and Opacity property you need at minimum a FrameworkElement. Control will still work for your purpose, but you might want to aim for the lowest common denominator. For example, a TextBlock is not a Control, it derives directly from FrameworkElement.

    The hierarchy of controls in WPF is:

    Object
    DispatcherObject
    DependencyObject
    Visual
    UIElement
    FrameworkElement
    Control
    

    From there it gets complicated. For instance:

    Button : ButtonBase : ContentControl : Control
    TextBox : TextBoxBase : Control
    ComboBox : Selector : ItemsControl : Control
    DataGrid : MultiSelector : Selector : ItemsControl : Control
    

    But they all derive from Control which derives from FrameworkElement.

    The simplest way to find out the inheritance of a type is to put the caret on the type in your code and press F12. This gives you some pseudo-code describing the type (or actual code if it's available) and you can navigate through the base classes the same way.