获取样式以覆盖控件模板的某些设置? [英] getting a style to override some settings of a control template?

查看:88
本文介绍了获取样式以覆盖控件模板的某些设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对控件模板上的样式的层次感到有点困惑。

我有10个不同的按钮模板,但是我想应用一种调整宽度和高度的样式。这样我可以集体改变宽度和高度,同时仍然有非常独特的按钮,有自己的鼠标悬停事件等。但是,当我应用具有固定宽度的样式,没有任何反应 - 按钮仍然自动大小(这是我的宽度在控件模板中设置的内容)。

任何帮助将不胜感激。


Purkiss

解决方案



这应该按照您的预期工作。只需确保您在样式中设置的属性也未设置在按钮的实际实例上,因为这些属性将覆盖样式设置。
在下面的XAML中,如果您更改高度和宽度设置器的值样式然后两个按钮都正确更新。


<前lang = x-xml>< UserControl
xmlns =" http://schemas.microsoft.com/winfx/ 2006 / XAML /呈现"
xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm =" clr-namespace:System.Windows; assembly = System.Windows"的xmlns:对照= QUOT; CLR-命名空间:System.Windows.Controls;装配= System.Windows.Controls"的xmlns:d =" HTTP://schemas.microsoft.com/expression/blend/2008"的xmlns:MC = QUOT; HTTP://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class =" SilverlightApplication59.MainPage"
Width =" 640"高度= QUOT; 480" MC:可忽略= QUOT; d">

< UserControl.Resources>
< ControlTemplate x:Key =" ButtonControlTemplate1"的TargetType = QUOT;按钮和QUOT;>
< Grid>
< Rectangle Fill =" Red"行程="黑色" />
< / Grid>
< / ControlTemplate>
< ControlTemplate x:Key =" ButtonControlTemplate2"的TargetType = QUOT;按钮和QUOT;>
< Grid>
< Rectangle Fill ="#FFEF00FF"行程="黑色"半径X = QUOT; 27"半径Y = QUOT; 27" />
< / Grid>
< / ControlTemplate>
< Style x:Key =" ButtonStyle1"的TargetType = QUOT;按钮和QUOT;>
< Setter Property =" Width"值= QUOT; 100" />
< Setter Property =" Height"值= QUOT; 100" />
< / Style>
< /UserControl.Resources>
< Grid x:Name =" LayoutRoot"背景= QUOT;白色">
< Button Template =" {StaticResource ButtonControlTemplate1}" Style =" {StaticResource ButtonStyle1}"含量="按钮和QUOT; d:LayoutOverrides = QUOT;宽度"的Horizo​​ntalAlignment = QUOT;左"余量= QUOT; 95,190,0,190" />
< Button Template =" {StaticResource ButtonControlTemplate2}" Style =" {StaticResource ButtonStyle1}"含量="按钮和QUOT; d:LayoutOverrides = QUOT;宽度"的Horizo​​ntalAlignment = QUOT;右"余量= QUOT; 0190125190" />
< / Grid>
< / UserControl>


谢谢!


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.


Purkiss

解决方案

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.

<UserControl
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	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"
	x:Class="SilverlightApplication59.MainPage"
	Width="640" Height="480" mc:Ignorable="d">

	<UserControl.Resources>
		<ControlTemplate x:Key="ButtonControlTemplate1" TargetType="Button">
			<Grid>
				<Rectangle Fill="Red" Stroke="Black"/>
			</Grid>
		</ControlTemplate>
		<ControlTemplate x:Key="ButtonControlTemplate2" TargetType="Button">
			<Grid>
				<Rectangle Fill="#FFEF00FF" Stroke="Black" RadiusX="27" RadiusY="27"/>
			</Grid>
		</ControlTemplate>
		<Style x:Key="ButtonStyle1" TargetType="Button">
			<Setter Property="Width" Value="100"/>
			<Setter Property="Height" Value="100"/>
		</Style>
	</UserControl.Resources>
	<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"/>
	</Grid>
</UserControl>

Thanks!


这篇关于获取样式以覆盖控件模板的某些设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆