Set a value to a control only in design mode

I have been working on making my project localizable. I made it using Resources just like skins but this will be another post.

The final problem I encountered was that there was no design time support to show a dummy text, in a Button for example. Everything was going well except from the design part which was not that significant for me, but as I’m the writer of the company’s new framework it might be for others.

So can you set a value to DependencyProperty of a DependencyObject only to in DesignMode?

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;
    }

}

And this is how you can use it

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

or

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

where CoreME is an xmlns defined namespace shortcut

 

About these ads

2 comments

  1. Pingback: ALEX TO » Blog Archive » Set a value to a control only in design mode


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s