getting a style to override some settings of a control template? RRS feed

  • Question

  • I'm a little confused on the heirarchy of a style over a control template.

    I have 10 different button templates, but I would like to apply a single style that adjusts the width and height.  This way i can change the width and height en masse, while still having very unique buttons that have their own mouseover events etc.  however, when i apply the style with a fixed width, nothing happens--the button still auto sizes (which is what my width is set to in the control template). 

    Any help would be appreciated.
    Thursday, April 16, 2009 8:17 PM

All replies

  • Hi,

    This should work as you expect. Just make sure that the properties you set in the Style are not also set on actual instances of the Buttons since those will override the Style settings.
    In the XAML below if you change the values of the Height and Width Setters in the Style then both Buttons update correctly.

    	xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    	Width="640" Height="480" mc:Ignorable="d">
    		<ControlTemplate x:Key="ButtonControlTemplate1" TargetType="Button">
    				<Rectangle Fill="Red" Stroke="Black"/>
    		<ControlTemplate x:Key="ButtonControlTemplate2" TargetType="Button">
    				<Rectangle Fill="#FFEF00FF" Stroke="Black" RadiusX="27" RadiusY="27"/>
    		<Style x:Key="ButtonStyle1" TargetType="Button">
    			<Setter Property="Width" Value="100"/>
    			<Setter Property="Height" Value="100"/>
    	<Grid x:Name="LayoutRoot" Background="White">
    		<Button Template="{StaticResource ButtonControlTemplate1}" Style="{StaticResource ButtonStyle1}" Content="Button" d:LayoutOverrides="Width" HorizontalAlignment="Left" Margin="95,190,0,190"/>
    		<Button Template="{StaticResource ButtonControlTemplate2}" Style="{StaticResource ButtonStyle1}" Content="Button" d:LayoutOverrides="Width" HorizontalAlignment="Right" Margin="0,190,125,190"/>
    Jeetendra Kukreja [MSFT]
    Tuesday, April 21, 2009 12:39 AM