Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

wpf - Create properties that only apply on design time

I am using visual studio dark theme. As a result when designing my views I cannot see the font if its black. A fix will be to set the background of the view to white. But our application has different themes so I cannot hard code that.

There are to great properties that I use when creating an usercontrol:

d:DesignWidth="1110" d:DesignHeight="400"

those properties are only affecting the view at design time. It will be great if I can create a property d:DesignBackground just so that I do not have to be adding and removing the background property every time I run the application.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Not sure if it's exactly what you're looking for, but what I do is just plop a trigger in the app.xaml to invoke using the IsInDesignMode property like;

Namespace (Thanks Tono Nam);

xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=PresentationFramework"

XAML;

<Style TargetType="{x:Type UserControl}">
    <Style.Triggers>
        <Trigger Property="ComponentModel:DesignerProperties.IsInDesignMode"
                 Value="True">
            <Setter Property="Background"
                    Value="#FFFFFF" />
        </Trigger>
    </Style.Triggers>
</Style>

Simple, but works, and sometimes I target other dependency properties like font and stuff too depending on the need. Hope this helps.

PS - You can target other TargetType's with their own properties the same way, like for example, ChildWindows, Popups, Windows, whatever...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...