本文介绍了在WPF中使用触发器内部的控件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 我试图为一个按钮制作一个转换器,以便给它一个任意的背景,并且基于那个画笔(实际上,它支持只有SolidColorBrushes,LinearGradientBrushes和RadialGradientBrushes)。 我用过这段代码: < 样式 TargetType = 按钮 > < Setter 属性 = 模板 > < Setter.Value > < ControlTemplate TargetType = {x:Type Button} > < 边框 x:名称 = border BorderBrush = {TemplateBinding BorderBrush} BorderThickness = {TemplateBinding BorderThickness} 已删除 = {TemplateBinding Background} SnapsToDevicePixels = true CornerRadius = 5 > < ContentPresenter x:名称 = contentPresenter 可聚焦 = 错误 HorizontalAlignment = {TemplateBinding HorizontalContentAlignment} 保证金 = {TemplateBinding Padding} RecognizesAccessKey = True SnapsToDevicePixels = {TemplateBinding SnapsToDevicePixels} VerticalAlignment = {TemplateBinding VerticalContentAlignment} / > < / Border > < / ControlTemplate > < / Setter.Value > < / Setter > < 样式.Triggers > < 触发 属性 = IsMous eOver 值 = True > < Setter 属性 = 背景 值 = {Binding Path = Background,Converter = {StaticResource BTHC}} / > < / Trigger > < / Style.Triggers > < / Style > 在样式定义中按钮。错误发生在触发器中,但我无法理解......我不知道为什么它不起作用而且根本不会以任何方式修改背景... 你能帮我解决这个问题吗? 非常感谢! Lusvardi Gianmarco 解决方案 好的,我自己解决了这个问题: < style targettype = 按钮 > < setter property = 模板 > < setter.value > < controltemplate targettype = {x:Type Button} > < border x:name = border borderbrush = {TemplateBinding BorderBrush} borderthickness = {TemplateBinding BorderThickness} 背景 = {TemplateBinding Background} snapstodevicepixels = true cornerradius = 5 tag = {TemplateBinding Background} xmlns:x = #unknown > < contentpresenter x:name = contentPresenter focusable = False horizontalalignment = {TemplateBinding HorizontalContentAlignment} margin = {TemplateBinding Padding} recognaccesskey = True snapstodevicepixels = {TemplateBinding SnapsToDevicePixels} verticalalignment = {TemplateBinding VerticalContentAlignment} / > < / border > < controltemplate.triggers > < 触发器 property = IsMouseOver value = True > < setter property = 背景 targetname = border value = {Binding ElementName = border,Path = Tag,Converter = {StaticResource BTHC}} / > < / trigger > < trigger property = IsPressed value = True > < setter property = 背景 targetname = border value = {Binding ElementName = border,Path = Tag,Converter = {StaticResource BTPC } / > < / trigger > < 触发器 property = IsEnabled value = False > < setter property = 背景 targetname = border value = {Binding ElementName = border,Path = Tag,Converter = {StaticResource BTDC}} / > < / trigger > < / controltemplate.triggers > < / controltemplate > < / setter.value > < / setter > < / style > Hi everybody,I tried to make a converter for a button in order to give it an arbitrary background and, based on that brush (actually, it supports only SolidColorBrushes, LinearGradientBrushes and RadialGradientBrushes).I used this code:<Style TargetType="Button"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" removed="{TemplateBinding Background}" SnapsToDevicePixels="true" CornerRadius="5"> <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="{Binding Path=Background, Converter={StaticResource BTHC}}" /> </Trigger> </Style.Triggers> </Style>inside the style definition of the button. The error is in the triggers, but I cannot understand it... I do not know why it does not work and simply does not modify the background in any way...Could you please help me sorting out this problem?Thanks a lot!Lusvardi Gianmarco 解决方案 Ok, I've solved it by myself:<style targettype="Button"> <setter property="Template"> <setter.value> <controltemplate targettype="{x:Type Button}"> <border x:name="border" borderbrush="{TemplateBinding BorderBrush}" borderthickness="{TemplateBinding BorderThickness}" background="{TemplateBinding Background}" snapstodevicepixels="true" cornerradius="5" tag="{TemplateBinding Background}" xmlns:x="#unknown"> <contentpresenter x:name="contentPresenter" focusable="False" horizontalalignment="{TemplateBinding HorizontalContentAlignment}" margin="{TemplateBinding Padding}" recognizesaccesskey="True" snapstodevicepixels="{TemplateBinding SnapsToDevicePixels}" verticalalignment="{TemplateBinding VerticalContentAlignment}" /> </border> <controltemplate.triggers> <trigger property="IsMouseOver" value="True"> <setter property="Background" targetname="border" value="{Binding ElementName=border, Path=Tag, Converter={StaticResource BTHC}}" /> </trigger> <trigger property="IsPressed" value="True"> <setter property="Background" targetname="border" value="{Binding ElementName=border, Path=Tag, Converter={StaticResource BTPC}}" /> </trigger> <trigger property="IsEnabled" value="False"> <setter property="Background" targetname="border" value="{Binding ElementName=border, Path=Tag, Converter={StaticResource BTDC}}" /> </trigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter></style> 这篇关于在WPF中使用触发器内部的控件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-12 00:33