2,000 Things You Should Know About WPF

Everything a WPF Developer Needs to Know, in Bite-Sized Chunks

  • Home
  • About
  • Index
Posts Comments
  • Controls
  • Layout
  • Events
  • Basics
  • Blend
  • Graphics
  • XAML
  • Miscellaneous
  • Dependency Properties
  • Visual Studio

#166 – You Can Override Metadata for Any Dependency Property

December 25, 2010 1 Comment

Recall that you can set a dependency property value on a DependencyObject for any dependency property, not just the properties that the class implements or inherits.

Since any dependency property can be attached to any dependency object, you can override metadata for any dependency property in a class.

Here’s an example where we override the Grid.Row and Grid.Column properties in a class that derives from Button.  If this ButtonLoner object is defined in a Grid in XAML, it will automatically appear in Row 1, Col 1, rather than Row 0, Col 0.

    public class ButtonLoner : Button
    {
        static ButtonLoner()
        {
            Grid.RowProperty.OverrideMetadata(typeof(ButtonLoner), new FrameworkPropertyMetadata(1));
            Grid.ColumnProperty.OverrideMetadata(typeof(ButtonLoner), new FrameworkPropertyMetadata(1));
        }
    }

Here’s an example of the button being used:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <app:ButtonLoner x:Name="btnLoner" Content="Loner" Width="100" Height="25"/>
    </Grid>

Advertisement

Filed under Dependency Properties Tagged with Attached Properties, Dependency Properties, OverrideMetadata, WPF

#165 – Overriding Metadata Can Result in Merged Metadata

December 24, 2010 Leave a comment

If you override metadata for a dependency property that a class inherits from its parent, you can specify one or more things to override:

  • The default value
  • The PropertyChangedCallback
  • The CoerceValueCallback
  • Other flags specified for UIPropertyMetadata or FrameworkPropertyMetadata

If you don’t specify all of these things, however, the items that you do not specify will be inherited from the parent.

For example, we created a child class of Slider and overrode metadata to provide a new default value for the Maximum property.  But the coercion behavior of Maximum in our new class behaves as it does for Slider.Maximum.  (Maximum can’t be set below Minimum).

Because we didn’t specify a CoerceValueCallback, the existing coercion logic is used.

When you do specify new metadata:

  • New default value replaces the existing one
  • PropertyChangedCallback is merged with the existing one  (both are invoked)
  • CoerceValueCallback replaces the existing one

Filed under Dependency Properties Tagged with CoerceValueCallback, Dependency Properties, OverrideMetadata, PropertyChangedCallback, WPF

#164 – Overriding Metadata for an Inherited Dependency Property

December 23, 2010 3 Comments

When deriving a new child class, you inherit the parent’s dependency properties.  You might also want to change the behavior of one or more of the dependency properties.  You can do this to some extent by overriding the metadata for a dependency property.

Overriding the metadata in the child class doesn’t change the behavior of the dependency property in the parent class.

In the example below, we create a new class and override metadata for several properties, changing the default values.

    public class ThermometerSlider : Slider
    {
        static ThermometerSlider()
        {
            // Defaults for standard Slider: Min = 0, Max = 10, Value = 0
            // Defaults for ThermometerSlider: Min = -40, Max = 120, Value = 70

            // Change default Minimum to -40.0
            MinimumProperty.OverrideMetadata(typeof(ThermometerSlider), new FrameworkPropertyMetadata(-40.0));

            // Change default Maximum to 120
            MaximumProperty.OverrideMetadata(typeof(ThermometerSlider), new FrameworkPropertyMetadata(120.0));

            // Change default Value to 70
            ValueProperty.OverrideMetadata(typeof(ThermometerSlider), new FrameworkPropertyMetadata(70.0));
        }
    }

The metadata type must match the type in the original property (e.g. FrameworkPropertyMetadata, UIPropertyMetadata, or PropertyMetadata).

Filed under Dependency Properties Tagged with Dependency Properties, OverrideMetadata, WPF

Sean Sexton

Recent Posts

  • #1,219 – Expanding All Nodes in a TreeView by Default
  • #1,218 – Stretching Items in TreeView across Entire Control
  • #1,217 – Using Multiple HierarchicalDataTemplates in a TreeView
  • #1,216 – Creating a Custom ItemTemplate in a TreeView
  • #1,215 – Binding a TreeView to a Hierarchical Data Source

Blogroll

  • 2,000 Things You Should Know About C#
  • Britannica Geek
  • Sean on Twitter
  • Sean's Stuff

Calendar

February 2023
S M T W T F S
 1234
567891011
12131415161718
19202122232425
262728  
« Sep    

Top Posts

  • #220 - Using the Predefined Colors
  • #1,107 - Accessing an Embedded Resource Using a Uri
  • #210 - Specifying Colors in XAML As RGB Values
  • #1,219 - Expanding All Nodes in a TreeView by Default
  • Index
  • #351 - Binding a CheckBox's IsChecked Property to a Boolean Variable
  • #1,204 - Using a DataTrigger to Change Content in a ContentPresenter
  • #361 - Creating a ToggleButton Whose Content Is an Image
  • #670 - Getting the Mouse Position Relative to a Specific Element
  • #400 - Using a WrapPanel as the Items Panel for a ListBox

Tags

Application Background Basics Binding Blend Border Brush Button Calendar Canvas CheckBox Color Colors ComboBox Commands ContentControl Controls Cursor Data Binding Dependency Properties Dependency property DockPanel Drag-and-Drop Events Expander FlowDocument Focus Fonts FrameworkElement Gradient Graphics Grid GridSplitter GroupBox HorizontalAlignment Image InkCanvas Input ItemsControl Keyboard Keyboard Focus KeyDown KeyUp Layout LinearGradientBrush ListBox Localization Manipulation Margin Measure Miscellaneous Mouse Panel Popup Radial Gradient Resources Routed Events ScrollViewer Shape Slider StackPanel TabControl Text TextBlock TextBox Tooltip Touch Input Transforms UIElement Visual Studio Window Windows WPF WrapPanel XAML

Blog Stats

  • 5,148,295 hits

Create a free website or blog at WordPress.com.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Follow Following
    • 2,000 Things You Should Know About WPF
    • Join 290 other followers
    • Already have a WordPress.com account? Log in now.
    • 2,000 Things You Should Know About WPF
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...