C# 更改wpf按钮后,触发颜色无效
这是我的按钮代码C# 更改wpf按钮后,触发颜色无效,c#,wpf,C#,Wpf,这是我的按钮代码 public class MetroButton : Button { public static readonly DependencyProperty MoseOverBrushProperty; public static readonly DependencyProperty PressedBrushProperty; public MetroButton():base() { var resource = new
public class MetroButton : Button
{
public static readonly DependencyProperty MoseOverBrushProperty;
public static readonly DependencyProperty PressedBrushProperty;
public MetroButton():base()
{
var resource = new ResourceDictionary
{
Source = new Uri("/Parking.Component.Ui;component/Styles/ButtonMetro.xaml",
UriKind.RelativeOrAbsolute)
};
Style = resource["ButtonMetro"] as Style;
//SetResourceReference(StyleProperty, Style);
}
static MetroButton()
{
MoseOverBrushProperty = DependencyProperty.Register("MoseOverBrush", typeof(Brush), typeof(MetroButton));
PressedBrushProperty = DependencyProperty.Register("PressedBrush", typeof(Brush), typeof(MetroButton));
}
public Brush MoseOverBrush
{
get { return (Brush)base.GetValue(MoseOverBrushProperty); }
set { base.SetValue(MoseOverBrushProperty, value); }
}
public Brush PressedBrush
{
get { return (Brush)base.GetValue(PressedBrushProperty); }
set { base.SetValue(PressedBrushProperty, value); }
}
}
我用这个样式做我的按钮
<Style x:Key="ButtonMetro" TargetType="{ x:Type LochalUI:MetroButton}">
<Setter Property="Foreground" Value="White" />
<Setter Property="MoseOverBrush" Value="#FF3F62FD"/>
<Setter Property="PressedBrush" Value="#FF000099"/>
<Setter Property="Background" Value="#FF6B9AFF"/>
<Setter Property="FontSize" Value="15" />
<Setter Property="FontFamily" Value="B Yekan" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type LochalUI:MetroButton}">
<Border x:Name="border" CornerRadius="4" Background="{TemplateBinding Background}">
<Grid>
<ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,0" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{Binding Path=MoseOverBrush , RelativeSource={RelativeSource Self}}" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{Binding Path=PressedBrush , RelativeSource={RelativeSource Self}}" />
<Setter Property="Foreground" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但当我为按钮背景添加颜色时,问题就出现了,如下面的代码所示:
<UI:MetroButton HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="132" Height="107" Background="#FF09CD00" >
<Grid>
<Label Content="تنظیمات" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="5,5,5,12" Foreground="White" Margin="5"/>
</Grid>
</UI:MetroButton>
iMouseOver变换器颜色和iPressed触发器不工作。
(我不想在setters中使用静态资源)
更改其他属性没有任何效果,只是更改背景造成了此问题。我发现问题的答案在两个位置: 第一个是当我们在中使用触发器时
<ControlTemplate.Triggers/>
到
因为
MoseOverBrush
是parrent属性而不是ControlTemplate属性,所以样式触发器只能更改样式中设置的属性,而不能更改标记属性。您需要从UI:MetroButton
标记中删除Background=“#FF09CD00”
。@TeaDrivenDev如果您输入触发器静态值,这将是一个很好的测试代码,在给出负值之前我找到了解决方案,您缺少TargetName=“border”
由于该样式试图应用于文本块
而非边框
@XAMlMAX,请在鼠标悬停时尝试将代码更改为白色,而不是我们在鼠标悬停时设置的颜色brush@XAMlMAX谢谢你们,弗雷尔,其中一个问题是你们以边境的名义说的其中一个问题,另一个问题是绑定问题
Value="{Binding Path=MoseOverBrush , RelativeSource={RelativeSource Self}}"
Value="{Binding Path=MoseOverBrush , RelativeSource={RelativeSource TemplatedParent}}"