Click here to Skip to main content
Licence CPOL
First Posted 13 Jun 2008
Views 9,005
Bookmarked 4 times

Two Help Classes for WPF Regarding Design

By Sarafian | 13 Jun 2008
Check if the code is runing from the Designer and set values only to be seen in the Designer.
1 vote, 33.3%
1
2 votes, 66.7%
2

3

4

5
1.80/5 - 3 votes
μ 1.80, σa 1.01 [?]

Introduction

This article shows a combination of two helper classes I wrote for WPF.

Background

For various reasons, I needed to set values to an element only to be shown in the Designer. This is done by the first class. The second class is used by the first to check whether code is running under the Designer.

Set a value to a control only in Design mode

Can you set a value to the DependencyProperty of a DependencyObject in Design mode? Using a custom MarkupExtension, you can. Here is the implementation:

[ContentProperty("Value")]
public class DesignTimeDummy : MarkupExtension
{
    public DesignTimeDummy()
    {
    }

    public object Value { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (Helpers.Designer.InDesignMode)
        {
            return Value;
        }

        return DependencyProperty.UnsetValue;
    }
}

Here are two examples for the XAML code:

<Button Style="{DynamicResource styleName}" 
   Content="{CoreME:DesignTimeDummy Value=DesignContentValue}"/>

or

<Button Style="{DynamicResource styleName}" >
<Button.Content>
<CoreME:DesignTimeDummy>
Dummy </CoreME:DesignTimeDummy>
</Button.Content>
</Button>

where CoreME is an xmlns defined namespace shortcut.

Check to see if it is in Design mode

The above code uses this class:

public static class Designer
{
    private static DependencyObject dummy = new DependencyObject();
    public static bool InDesignMode
    {
        get { return DesignerProperties.GetIsInDesignMode(dummy); }
    }
}

Hope you find them useful.

Blog links

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sarafian

Team Leader
ALGOSYSTEMS
Greece Greece

Member
I live in Athens Greece and currently I am working with Business scale application with .NET latest technologies
 
I've been developing applications for personal and friends usage with C++ using majorly Borland's various IDEs since 1994.
In 2002 I began working for an R&D institute where I was introduced to C# which I worships ever since.
 
I love core application development and I would like to publish more articles here and on my blog, but there is not enough time to do so.
 
I usualy "waste" my spare time watching sitcoms, preferable SCI-FI.
I would like to play chess but I can't find any real world players to hang out with.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberganggang6:28 8 Jan '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 13 Jun 2008
Article Copyright 2008 by Sarafian
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid