问题描述:

I have one windows form application in visual studio 2013 , C# language, I want to define something like CSS file , which is defined for web apps , but for my application.

So as a result I want to have one app that when I run application , all the forms and buttons will have same colors and are got their styles from the common file.

Is it possible? if so, how can I do that?

thanks in advance

网友答案:

WinForm: For winForm application you should create your own custom controls, from scratch or derived from existing one. Probably someone has already done it, like DevExpress. If you are not planning to purchase anything, I suggest to use WPF.

WPF: In order to make a theme you have to make a ResourceDictionary with a list of Styles for a different controls.

So, create a new ResourceDictionary, implement a lot of styles for particular controls. For example, style for RepeatButton

<Style x:Key="TestStyle" d:IsControlPart="True" TargetType="{x:Type RepeatButton}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="IsTabStop" Value="false" />
    <Setter Property="Focusable" Value="false" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RepeatButton}">
                <Grid>
                    <Rectangle Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Then in your application add a reference to the ResourceDictionary.

<Application x:Class="ThemesSample.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <ResourceDictionary Source="ExpressionDark.xaml"/>
    </Application.Resources>
</Application>

You can find some examples here

相关阅读:
Top