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

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

September 2014
S M T W T F S
 123456
78910111213
14151617181920
21222324252627
282930  
« Aug   Oct »

Top Posts

  • #220 - Using the Predefined Colors
  • Index
  • #351 - Binding a CheckBox's IsChecked Property to a Boolean Variable
  • #1,204 - Using a DataTrigger to Change Content in a ContentPresenter
  • #210 - Specifying Colors in XAML As RGB Values
  • #854 - Clicked vs. Checked/Unchecked Events for CheckBox
  • #1,107 - Accessing an Embedded Resource Using a Uri
  • #588 - If You Handle PreviewKeyDown Event, KeyDown Won't Fire
  • #1,062 - Scaling a Canvas Using a ViewBox
  • #376 - Getting Text to Wrap in a Tooltip

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,176,363 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 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