How To: Use Windows.Resources and Resource.Dictionary for Setting Default Styles for ToolTip and Button Controls

 

<Window.Resources/>

In the XAML file you are working on add the following code if the Resource.Dictionary is named StylesDictionary.xaml and is located in a root directory called Styles.  The Project name is called MyProjectWPF_GUI.

Code Snippet
  1.  
  2. <Window.Resources>
  3.     <ResourceDictionary>
  4.         <ResourceDictionary.MergedDictionaries>
  5.             <ResourceDictionary Source="pack://application:,,,/MYProjectWPF_GUI;component/Styles/StylesDictionary.xaml" />
  6.         </ResourceDictionary.MergedDictionaries>
  7.     </ResourceDictionary>
  8. </Window.Resources>

 

Note:

The Source  = pack://application:,,,/MyProjectWPF_GUI;component/Styles/StylesDictionary.xaml”

needs to have this “pack” syntax if you have a namespace declaration for “UserControls” like is used in this Window.

xmlns:local="clr-namespace:MyProjectWPF_GUI" 

The error at runtime will be: “Set property ‘System.Windows.ResourceDictionary.Source’ threw an exception.”

 

Otherwise if you do not declare a namespace for a “UserControl” then you can call it by

Source=”/Styles/StylesDictionary.xaml”

Code Snippet
  1. <Window x:Class="MobiusWindow"
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
  4.     WindowStartupLocation="CenterOwner"
  5.     ResizeMode="NoResize"
  6.     Title="Submit to Mobius" Height="480" Width="647"
  7.       xmlns:local="clr-namespace:MYProjectWPF_GUI"  
  8.     Name="MobiusWin">

 

<Resource.Dictionary/>

The Resource.Dictionary looks like the following.  Its filename is called StylesDictionary.xaml and is under a directory called Styles.

Code Snippet
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  2.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  3.  
  4.     <!– Default Button Style–>
  5.           <Style TargetType="Button">
  6.             <Setter Property="FontWeight" Value="Bold" />
  7.         </Style>
  8.     
  9.     <!– Default ToolTip tyle–>
  10.         <Style x:Key="{x:Type ToolTip}" TargetType="ToolTip">
  11.             <Setter Property="OverridesDefaultStyle" Value="true"/>
  12.             <Setter Property="HasDropShadow" Value="True"/>
  13.             <Setter Property="Template">
  14.                 <Setter.Value>
  15.                     <ControlTemplate TargetType="ToolTip">
  16.                         <Border CornerRadius="7" HorizontalAlignment="Center" VerticalAlignment="Top" Padding="5" BorderThickness="3,3,3,3">
  17.                             <Border.Background>
  18.                                 <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
  19.                                     <GradientStop Color="#CF181818" Offset="0"/>
  20.                                     <GradientStop Color="#BE1C1C1C" Offset="1"/>
  21.                                 </LinearGradientBrush>
  22.                             </Border.Background>
  23.                             <Border.BorderBrush>
  24.                                 <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
  25.                                     <GradientStop Color="#80FFFFFF" Offset="0"/>
  26.                                     <GradientStop Color="#7FFFFFFF" Offset="1"/>
  27.                                     <GradientStop Color="#FFFFF18D" Offset="0.344"/>
  28.                                     <GradientStop Color="#FFFFF4AB" Offset="0.647"/>
  29.                                 </LinearGradientBrush>
  30.                             </Border.BorderBrush>
  31.                             <StackPanel>
  32.                                 <TextBlock FontFamily="Tahoma" FontSize="11" Text="{TemplateBinding Content}" Foreground="#FFFFFFFF" />
  33.                             </StackPanel>
  34.                         </Border>
  35.                     </ControlTemplate>
  36.                 </Setter.Value>
  37.             </Setter>
  38.         </Style>
  39. </ResourceDictionary>

 

Good Luck

Tags: , , , ,

7 Responses to “How To: Use Windows.Resources and Resource.Dictionary for Setting Default Styles for ToolTip and Button Controls”

  1. find url Says:

    search engine

  2. besi baja Says:

    upgrade use g theirof ardmi can insult tepat orhave is andbe pres

  3. film festival panel discussions Says:

    Hi there i am kavin, its my first occasion to commenting anywhere, when i read this piece of writing i thought i could also
    create comment due to this good article.

  4. Mannheim Kindergeburtstag Says:

    Everything is very open with a very clear clarification of
    the challenges. It was truly informative.
    Your website is extremely helpful. Thank you for
    sharing!

  5. funny gifts Says:

    Hi it’s me, I am also visiting this site regularly, this website is in fact fastidious and the viewers are genuinely sharing pleasant thoughts.

  6. Kindergeburtstag Mannheim Says:

    Very good information. Lucky me I ran across your site by
    accident (stumbleupon). I’ve book-marked it for later!

  7. dictionar | enciclopedia | dex Says:

    dictionar | enciclopedia | dex…

    […]How To: Use Windows.Resources and Resource.Dictionary for Setting Default Styles for ToolTip and Button Controls « 1-800-The-Nerd's Blog[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: