使用ivalueconverter进行模板键入
我想在WPF项目中生成一个自定义按钮。我希望按钮以这种方式更改其背景颜色,以减少不透明度。
因此,我有一个带有默认按钮样式的资源词典:
<Style x:Key="DefaultStyle" TargetType="{x:Type Button}">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{Binding Path=Background, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource OpacityConverter}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="GreenButton" TargetType="{x:Type Button}" BasedOn="{StaticResource DefaultStyle}">
<Setter Property="Background" Value="{StaticResource Green}"/>
<Setter Property="Foreground" Value="Black"/>
</Style>
使用我的自定义转换器:
<conv:OpacityConverter x:Key="OpacityConverter"/>
在 xmlns中实现:cons =“ clr-namespace:projectNamespace.converter”:
public class OpacityConverter : IValueConverter
{
public OpacityConverter()
{
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush brush = ((SolidColorBrush)value).Clone();
brush.Opacity = 0.5;
return brush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush brush = ((SolidColorBrush)value).Clone();
brush.Opacity = 1;
return brush;
}
}
在我的窗口中,我使用了这样的按钮:
<Button Grid.Row="1" Grid.Column="2"
Style="{StaticResource GreenButton}">
<TextBlock Text="neues Projekt anlegen" TextWrapping="Wrap" TextAlignment="Center"/>
</Button>
按钮看起来像这样:
我想让这样的外观(如果Ismouseover == true):
但是,此刻,使用ismouseover = = true
启动,背景完全白色,按钮看起来像这样:
“ https://i.sstatic.net/a8fes.png” alt =“在此处输入图像说明”>
我缺少什么?
I want to generate a custom button in a WPF-project. I want the button to change its background color in such a way, that the opacity is reduced.
Therefore I have a resource dictionary with a default button style:
<Style x:Key="DefaultStyle" TargetType="{x:Type Button}">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{Binding Path=Background, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource OpacityConverter}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="GreenButton" TargetType="{x:Type Button}" BasedOn="{StaticResource DefaultStyle}">
<Setter Property="Background" Value="{StaticResource Green}"/>
<Setter Property="Foreground" Value="Black"/>
</Style>
with my custom converter:
<conv:OpacityConverter x:Key="OpacityConverter"/>
implemented in xmlns:conv="clr-namespace:ProjectNamespace.Converter":
public class OpacityConverter : IValueConverter
{
public OpacityConverter()
{
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush brush = ((SolidColorBrush)value).Clone();
brush.Opacity = 0.5;
return brush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush brush = ((SolidColorBrush)value).Clone();
brush.Opacity = 1;
return brush;
}
}
In my Window I use the button like this:
<Button Grid.Row="1" Grid.Column="2"
Style="{StaticResource GreenButton}">
<TextBlock Text="neues Projekt anlegen" TextWrapping="Wrap" TextAlignment="Center"/>
</Button>
The button looks like this:
and I want to have a look like this (if IsMouseOver == True):
However in the moment, with IsMouseOver == True
starts, the background gets totally white and the button looks like this:
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您尚未为触发器中的设置器指定
targetName
,因此它隐含地涉及该按钮并导致圆形引用。要修复它,请命名边框并在setter中指定该名称。Since you haven't specify
TargetName
for Setter in Trigger, it implicitly refers to the Button and causes a circular reference. To fix it, name the Border and specify that name in Setter.