3

I really like the d:XYZ method for marking certain properties design time, such that they appear in Blend but not when my program is running for real

whats the opposite of d:? my goal is to have two different behaviors, one in design mode and the other when running for real?

For example, I'd like "TestTest" to appear Blend but "productionTest" to appear at runtime

<TextBlock x:Name="FileText" 
           TextWrapping="Wrap" 
           d:Text="TestTest" 
           Text="productionTest" 
           HorizontalAlignment="Center"/>
Noctis
  • 11,507
  • 3
  • 43
  • 82
stuck
  • 2,264
  • 2
  • 28
  • 62
  • 7
    Leaving d:? away is the opposite of having d:? :D :D – dev hedgehog Nov 09 '13 at 20:35
  • 2
    One strategy would be to use a Design Time view model that is shaped just like the runtime view model. Then bind it to the data context like... d:DataContext="{d:DesignData /SampleData/SerializedSampleData.xaml}" DataContext="{local:ViewModel}" Once you do that, everything bound to the datacontext will be the same. No changes necessary between design/runtime. – IntStarFoo Nov 16 '13 at 21:53
  • The code you list, does it work? What is wrong with it? – flup Nov 23 '13 at 17:00

3 Answers3

4

d is just a name of the Blend XML namespace: xmlns:d="http://schemas.microsoft.com/expression/blend/2008". It's not a generic "option", but a concrete namespace with concrete attributes: http://msdn.microsoft.com/en-us/library/ff602277(v=vs.95).aspx This is the exhaustive list and there are no opposites.

Ark-kun
  • 6,358
  • 2
  • 34
  • 70
3

Like dev hedgehog said ... the opposite would be: Not to use it.

The use of d: is for design mode, so you can have something like:

<Window ...
    Height="600"  Width="800"
    d:Height="200"  d:Width="300" >

And have a different design heigh/width for both design and runtime.

Some frameworks will actually help you out, and by doing something like (this example is specifically from the MVVM-Light framework):

DataContext="{Binding Main, Source={StaticResource Locator}}"

You could then have different values depending on the IsInDesignMode variable, so you could implement different data services that will feed dummy data to your design (let's say a list of random dates) and the real data to your run time (real dates from your database for example).
This way, you don't need to worry on each page with the design/runtime, since the framework will sort it out for you and feed you the right data.


Edit:
As per your comments ... You can't do that that way. You'll either have to relay on some coding (option 1, the longer), or some framework (option 2, better in my opinion).
In each case your XAML will look like:

<TextBlock x:Name="FileText" 
       TextWrapping="Wrap" 
       Text="{Binding BlendabilityText}" 
       />

You will need to have a public string BlendabilityText { get; set; } on your model of course.

Option 1:
You will need to create your view model with this property: 'public string BlendabilityText { get; set; }', and have 2 constructors. A default empty one, that is what being called when you're in design mode by default, in which you'll initialize your property like BlendabilityText = "TestTest"; In your second constructor, which you should be calling for the runtime mode, you should do the same with the BlendabilityText = "ProductionValue"; value.
Now you should see the right output.

Option 2: Using MVVM-Light framework, you could use the IsInDesignModeto initialize your property, putting this in your constructor:

if (IsInDesignMode)
{
    BlendabilityText = "TestTest";
}
else
{
    BlendabilityText = "ProductionTest";
}

Now, your string get bounded to the correct value depending on the framework's IsInDesignMode.

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • Hey, thanks Noctis I updated my question to be a little clearer - any tips how to make the test text appear? – stuck Nov 16 '13 at 21:35
2

You could use the below class

public class Design : MarkupExtension
{
    readonly object realValue;
    readonly object designValue;

    public Design(object realValue, object designValue)
    {
        this.realValue = realValue;
        this.designValue = designValue;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
        if (target != null && target.TargetObject != null)
        {
            var obj = target.TargetObject as DependencyObject;
            if (obj != null && DesignerProperties.GetIsInDesignMode(obj))
                return designValue;
        }
        return realValue;
    }
}

like this

<TextBlock Text="{my:Design 'Real value', 'Design value'}" />

However, this still has a (small) performance overhead (since this wont be excluded from compilation like d:XYZ is)

Olivier
  • 5,578
  • 2
  • 31
  • 46