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

#1,154 – Providing Default Values for Standard Dependency Properties in Custom Controls

September 9, 2014 1 Comment

When you define a custom control, you may define some custom dependency properties. For each property that you define, you specify metadata for your new dependency property, including default values.

You may also want to specify default values for dependency properties used by your class, but defined elsewhere.  In the code fragment below, we’re defining a custom shape that inherits from the Shape class.  We don’t want the user to have to provide values for the Stroke and Fill properties, so we use the OverrideMetadata method to provide default values.  Note that the user can still provide their own values for these properties.

    public class PieSlice : Shape
    {
        static PieSlice()
        {
            Brush myGreenBrush = new SolidColorBrush(Color.FromArgb(255, 6, 176, 37));
            myGreenBrush.Freeze();

            StrokeProperty.OverrideMetadata(
                typeof(PieSlice),
                new FrameworkPropertyMetadata(myGreenBrush));
            FillProperty.OverrideMetadata(
                typeof(PieSlice),
                new FrameworkPropertyMetadata(myGreenBrush));
        }

        // Remainder of custom control here..
    }
Advertisement

Filed under Graphics Tagged with Dependency Properties, Graphics, Metadata, WPF

#163 – Constructing FrameworkPropertyMetadata

December 22, 2010 Leave a comment

When you look at the metadata for a dependency property that is storing its metadata as an instance of FrameworkPropertyMetadata, you’ll see a series of boolean properties.

As a convenience, when constructing an instance of FrameworkPropertyMetadata, you don’t have to pass all of these boolean values into the constructor individually.  Instead, the constructor accepts a flags parameter that is a logical OR of a series of enumeration values.  The individual enumeration values come from the FrameworkPropertyMetadataOptions enum.

        // Register ZHeightProperty dependency property
        private static FrameworkPropertyMetadata meta = new FrameworkPropertyMetadata(1,   // default = 1
            FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure |
            FrameworkPropertyMetadataOptions.AffectsParentArrange | FrameworkPropertyMetadataOptions.AffectsParentMeasure |
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);

        public static readonly DependencyProperty ZHeightProperty =
            DependencyProperty.Register("ZHeightProperty", typeof(int), typeof(ThreeDButton), meta);

Filed under Dependency Properties Tagged with Dependency Properties, FrameworkPropertyMetadata, Metadata, WPF

#161 – Read a Dependency Property’s Metadata

December 20, 2010 Leave a comment

You can read a dependency property’s metadata by using the DependencyProperty.GetMetadata method.

For example, assume that we have a Dog class with a BirthYear dependency property and that the class has exposed BirthYearProperty as a public member.  We can retrieve the property’s metadata as follows:

            Dog fido = new Dog("Fido");
            PropertyMetadata meta = Dog.BirthYearProperty.GetMetadata(fido);

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

#89 – Using Visual Studio to Follow the Inheritance Chain for WPF Types

October 9, 2010 Leave a comment

The inheritance chain for WPF classes is pretty deep.  The Button class, for example, inherits from ButtonBase, and then from ContentControl, Control, FrameworkElement, etc.

You can look classes up in the MSDN documentation to find out which classes a particular class inherits from and which properties and methods it inherits.  Or you can use the Go To Definition feature in Visual Studio 2010 to follow the chain.

Assume you have some code that mentions the Button class.  You can right-click on the word “Button” and select Go To Definition.

You’ll see the metadata for Button and that it inherits from ButtonBase.

To follow the chain, right-click on ButtonBase and select Go To Definition again.

Again, a metadata window comes up, this time showing you the metadata for the ButtonBase class–and the fact that ButtonBase inherits from ContentControl.

The containing DLL is also listed at the top of the window.

Filed under Visual Studio Tagged with Inheritance, Metadata, Visual Studio

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

January 2023
S M T W T F S
1234567
891011121314
15161718192021
22232425262728
293031  
« Sep    

Top Posts

  • #220 - Using the Predefined Colors
  • #1,219 - Expanding All Nodes in a TreeView by Default
  • Index
  • #351 - Binding a CheckBox's IsChecked Property to a Boolean Variable
  • #845 - Display Ellipsis in TextBlock to Indicate that Content Doesn't Fit
  • #210 - Specifying Colors in XAML As RGB Values
  • #400 - Using a WrapPanel as the Items Panel for a ListBox
  • #1,107 - Accessing an Embedded Resource Using a Uri
  • #1,204 - Using a DataTrigger to Change Content in a ContentPresenter
  • #1,012 - Using a Different Data Template for the Face of a ComboBox

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,142,419 hits

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 289 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...