Style multi-file solution in WPF (XAML)

Source: Internet
Author: User
Style multi-file solution (XAML) http://chr1syu.spaces.live.com/Blog/cns in WPF! 6a09cb72c98e0121! 195 simple style notes in entrywpf (XAML)
  • Create Resources
    1. In Solution Explorer, you can add an item called resource dictionary to a project. This item adds a XAML file with the label <resourcedictionary> as root to your project.
    2. Various resources can be defined by using style, solidcolorbrush, and so on in the above files.
      For example: <solidcolorbrush X: Key = "normalbrush" color = "# fcf3d6"/>
      In this way, a paint resource whose key is normalbrush is defined.
    3. If you want to define a style, it is a little effort-consuming. Style can be attached with a key or not. However, if there is no key, targettype is generally used to indicate the default style for a specific type. Example:
      <Style X: Key = "buttonfocusvisual">
      <Setter property = "control. template">
      <Setter. value>
      <Controltemplate>
      <Border>
      <Rectangle margin = "2" strokethickness = "1"
      Stroke = "orange" strokedasharray = "1 2"/>
      </Border>
      </Controltemplate>
      </Setter. value>
      </Setter>
      </Style>
    4. For style, you can also define its trigger to allow the control to change its appearance based on some attribute changes. The example is as follows:
      <Style targettype = "button">
      <Setter property = "snapstodevicepixels" value = "true"/>
      <Setter property = "overridesdefaultstyle" value = "true"/>
      <Setter property = "focusvisualstyle"
      Value = "{staticresource buttonfocusvisual}"/>
      <Setter property = "template">
      <Setter. value>
      <Controltemplate targettype = "button">
      <Border
      X: Name = "border" cornerradius = "2"
      Borderthickness = "1"
      Background = "{staticresource normalbrush }"
      Borderbrush = "{staticresource normalborderbrush}">
      <Contentpresenter
      Margin = "2"
      Horizontalalignment = "center"
      Verticalalignment = "center"
      Recognizesaccesskey = "true"/>
      </Border>
      <Controltemplate. triggers>
      <Trigger property = "iskeyboardfocused" value = "true">
      <Setter targetname = "border"
      Property = "borderbrush"
      Value = "{staticresource defaultedborderbrush}"/>
      </Trigger>

      ............

      </Controltemplate. triggers>
      </Controltemplate>
      </Setter. value>
      </Setter>
      </Style>

  • Application Resources
    1. First, add the above resources to the app. Open the app. XAML file and add the previously defined XAML file to the tag <application. Resources>. For example:
      <Application. Resources>
      <Resourcedictionary>
      <Resourcedictionary. mergeddictionaries>
      <Resourcedictionary source = "rescommon. XAML"/>
      .........
      </Resourcedictionary. mergeddictionaries>
      </Resourcedictionary>
      </Application. Resources>
    2. Then it can be called in any XAML or C # Code. Those with no name but targettype will be set to the default style of the corresponding type. The application method is as follows:
      Style = "{staticresource resourcekeyname }"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.