Not able to set the default style for the controls with generic.xaml
-
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,
-
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,
In the constructor for the control(s), try adding
DefaultStyleKey = typeof(MyCustomControlClass);
Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
In the constructor for the control(s), try adding
DefaultStyleKey = typeof(MyCustomControlClass);
Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
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:
-
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:
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
-
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
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:
-
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:
-
As a point of courtesy, you should include how you solved the problem for future readers.
-
As a point of courtesy, you should include how you solved the problem for future readers.