Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Not able to set the default style for the controls with generic.xaml

Not able to set the default style for the controls with generic.xaml

Scheduled Pinned Locked Moved WPF
wpfcomtutorialquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    salon
    wrote on last edited by
    #1

    I want to set the default styling in some controls and want to set all properties in generic.xaml. I am looking at folowing link http://www.codeproject.com/KB/silverlight/mediabutton.aspx[^] but if change some property for example

    <Setter property="Cursor" Value="Arrow" />

    to

    <Setter property="Cursor" Value="Hand" />

    Its not getting reflected when I run the application. No idea why? Does anybody have any idea? I have done a lot of brainstorming but not able to find........ :sigh: Thanks in advance,

    M 1 Reply Last reply
    0
    • S salon

      I want to set the default styling in some controls and want to set all properties in generic.xaml. I am looking at folowing link http://www.codeproject.com/KB/silverlight/mediabutton.aspx[^] but if change some property for example

      <Setter property="Cursor" Value="Arrow" />

      to

      <Setter property="Cursor" Value="Hand" />

      Its not getting reflected when I run the application. No idea why? Does anybody have any idea? I have done a lot of brainstorming but not able to find........ :sigh: Thanks in advance,

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      In the constructor for the control(s), try adding

      DefaultStyleKey = typeof(MyCustomControlClass);

      Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      S 1 Reply Last reply
      0
      • M Mark Salsbery

        In the constructor for the control(s), try adding

        DefaultStyleKey = typeof(MyCustomControlClass);

        Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        S Offline
        S Offline
        salon
        wrote on last edited by
        #3

        I have done that also but not able to achieve to get the default value....

        M 1 Reply Last reply
        0
        • S salon

          I have done that also but not able to achieve to get the default value....

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          What does the style look like? Can you show the code? It can't have a Name/Key and it needs to have the correct TargetType. The Build Action property for the generic.xaml file should be set to resource. Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          S 1 Reply Last reply
          0
          • M Mark Salsbery

            What does the style look like? Can you show the code? It can't have a Name/Key and it needs to have the correct TargetType. The Build Action property for the generic.xaml file should be set to resource. Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            S Offline
            S Offline
            salon
            wrote on last edited by
            #5

            My CS file is like this:

            using System;
            using System.Net;
            using System.Windows;
            using System.Windows.Controls;
            using System.Windows.Documents;
            using System.Windows.Ink;
            using System.Windows.Input;
            using System.Windows.Media;
            using System.Windows.Media.Animation;
            using System.Windows.Shapes;

            namespace MyCustomControls
            {
            public class MyControl: ContentControl
            {
            public string Content1
            {
            get { return (string)GetValue(ContentProperty1); }
            set { SetValue(ContentProperty1, value); }
            }

                public static readonly DependencyProperty ContentProperty1 =
                    DependencyProperty.RegisterAttached("Content", typeof(string), typeof(MyControl),
                    new PropertyMetadata("a"));
            
                
                public MyControl()
                    {
                        this.DefaultStyleKey = typeof(MyControl);
                    }
            }
            

            }

            Generic.xaml

            <ResourceDictionary
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:MyCustomControls;assembly=MyCustomControls">
            <Style TargetType="local:MyControl">
            <Setter Property="VerticalAlignment" Value="Bottom"/>
            <Setter Property="HorizontalAlignment" Value="Right"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="VerticalAlignment" Value="Bottom"/>
            <Setter Property="Content" Value="The Code Project"/>
            </ResourceDictionary>

            and the xaml file of the user control where I want to use the above custom control

            <UserControl x:Class="MyPortal.Controls.ctrlLogin"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Width="400" Height="140"
            xmlns:local="clr-namespace:MyCustomControls;assembly=MyCustomControls">

            <Grid x:Name="LayoutRoot" Width="400" Height="500" Background="Gray" HorizontalAlignment="Center">
            <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="480"/>
            </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="400" />
                </Grid.ColumnDefinitions>
            

            <local:MyControl Grid.Column="0" Grid.Row="0"/>
            </Grid>

            </UserControl>

            None of the default property which is there in the

            M 1 Reply Last reply
            0
            • S salon

              My CS file is like this:

              using System;
              using System.Net;
              using System.Windows;
              using System.Windows.Controls;
              using System.Windows.Documents;
              using System.Windows.Ink;
              using System.Windows.Input;
              using System.Windows.Media;
              using System.Windows.Media.Animation;
              using System.Windows.Shapes;

              namespace MyCustomControls
              {
              public class MyControl: ContentControl
              {
              public string Content1
              {
              get { return (string)GetValue(ContentProperty1); }
              set { SetValue(ContentProperty1, value); }
              }

                  public static readonly DependencyProperty ContentProperty1 =
                      DependencyProperty.RegisterAttached("Content", typeof(string), typeof(MyControl),
                      new PropertyMetadata("a"));
              
                  
                  public MyControl()
                      {
                          this.DefaultStyleKey = typeof(MyControl);
                      }
              }
              

              }

              Generic.xaml

              <ResourceDictionary
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:local="clr-namespace:MyCustomControls;assembly=MyCustomControls">
              <Style TargetType="local:MyControl">
              <Setter Property="VerticalAlignment" Value="Bottom"/>
              <Setter Property="HorizontalAlignment" Value="Right"/>
              <Setter Property="Foreground" Value="White"/>
              <Setter Property="VerticalAlignment" Value="Bottom"/>
              <Setter Property="Content" Value="The Code Project"/>
              </ResourceDictionary>

              and the xaml file of the user control where I want to use the above custom control

              <UserControl x:Class="MyPortal.Controls.ctrlLogin"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              Width="400" Height="140"
              xmlns:local="clr-namespace:MyCustomControls;assembly=MyCustomControls">

              <Grid x:Name="LayoutRoot" Width="400" Height="500" Background="Gray" HorizontalAlignment="Center">
              <Grid.RowDefinitions>
              <RowDefinition Height="20"/>
              <RowDefinition Height="480"/>
              </Grid.RowDefinitions>

                  <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="400" />
                  </Grid.ColumnDefinitions>
              

              <local:MyControl Grid.Column="0" Grid.Row="0"/>
              </Grid>

              </UserControl>

              None of the default property which is there in the

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              You didn't post a complete style example, so it's hard to guess what's wrong. Here's an example that works for me:

              <Style TargetType="local:MyControl">
                  <Setter Property="VerticalAlignment" Value="Bottom"/>
                  <Setter Property="HorizontalAlignment" Value="Right"/>
                  <Setter Property="VerticalAlignment" Value="Bottom"/>
                  <Setter Property="Foreground" Value="#FF0000FF"/>
                  <Setter Property="FontWeight" Value="Bold"/>
                  <Setter Property="FontStyle" Value="Italic"/>
                  <Setter Property="Cursor" Value="Hand"/>
                  <Setter Property="Content" Value="The Code Project"/>
                  <Setter Property="Template">
                      <Setter.Value>
                          <ControlTemplate TargetType="local:MyControl">
                              <TextBlock Text="{TemplateBinding Content}"
                                         Cursor="{TemplateBinding Cursor}"
                                         HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                                         VerticalAlignment="{TemplateBinding VerticalAlignment}"
                                         FontFamily="{TemplateBinding FontFamily}"
                                         FontSize="{TemplateBinding FontSize}"
                                         FontWeight="{TemplateBinding FontWeight}"
                                         FontStyle="{TemplateBinding FontStyle}"
                                         FontStretch="{TemplateBinding FontStretch}"
                                         Foreground="{TemplateBinding Foreground}"
                                         Margin="{TemplateBinding Padding}" />
                          </ControlTemplate>
                      </Setter.Value>
                  </Setter>
              </Style>
              

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              S 1 Reply Last reply
              0
              • M Mark Salsbery

                You didn't post a complete style example, so it's hard to guess what's wrong. Here's an example that works for me:

                <Style TargetType="local:MyControl">
                    <Setter Property="VerticalAlignment" Value="Bottom"/>
                    <Setter Property="HorizontalAlignment" Value="Right"/>
                    <Setter Property="VerticalAlignment" Value="Bottom"/>
                    <Setter Property="Foreground" Value="#FF0000FF"/>
                    <Setter Property="FontWeight" Value="Bold"/>
                    <Setter Property="FontStyle" Value="Italic"/>
                    <Setter Property="Cursor" Value="Hand"/>
                    <Setter Property="Content" Value="The Code Project"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="local:MyControl">
                                <TextBlock Text="{TemplateBinding Content}"
                                           Cursor="{TemplateBinding Cursor}"
                                           HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                                           VerticalAlignment="{TemplateBinding VerticalAlignment}"
                                           FontFamily="{TemplateBinding FontFamily}"
                                           FontSize="{TemplateBinding FontSize}"
                                           FontWeight="{TemplateBinding FontWeight}"
                                           FontStyle="{TemplateBinding FontStyle}"
                                           FontStretch="{TemplateBinding FontStretch}"
                                           Foreground="{TemplateBinding Foreground}"
                                           Margin="{TemplateBinding Padding}" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
                

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                S Offline
                S Offline
                salon
                wrote on last edited by
                #7

                Thanks for ur kind support. The problem is solved.... :-D

                D 1 Reply Last reply
                0
                • S salon

                  Thanks for ur kind support. The problem is solved.... :-D

                  D Offline
                  D Offline
                  Derek Greer
                  wrote on last edited by
                  #8

                  As a point of courtesy, you should include how you solved the problem for future readers.

                  S 1 Reply Last reply
                  0
                  • D Derek Greer

                    As a point of courtesy, you should include how you solved the problem for future readers.

                    S Offline
                    S Offline
                    salon
                    wrote on last edited by
                    #9

                    Hi Derek, I have solved this problem with the above code given by Mark...

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups