Set a value to a control only in design mode

June 14, 2008

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

 


In design mode

June 14, 2008

WPF project has begun and I have been continuously finding new things. I’m very excited, mainly because what I have accomplished to today. Maybe I will write about it.

Many thanks to fellows like Josh,Karl and Sasha for the knowledge I have acquired from them since Summer 2007.

One of the things I needed, for my next post, is to know if code is running in DesignMode or not.

So here is the class I wrote

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