我有一个自定义的玻璃状按钮,按下时会显示黑色背景。那没关系,但我希望能有更多的东西。我想,而不是将背景显示为黑色,将背景显示为从按钮的实际背景颜色派生的东西。
是否可以从XAML调用函数,传递背景颜色,并使用触发器中的返回值?
这就是我现在所拥有的
<Trigger Property="IsPressed" Value="True">
<Setter Property="Visibility" TargetName="Glow" Value="Hidden"/>
<Setter Property="Opacity" TargetName="Shine" Value="0.4"/>
<Setter Property="Background" TargetName="Border" Value="#CC000000"/>
</Trigger>
这就是我想要的 -
<Trigger Property="IsPressed" Value="True">
<Setter Property="Visibility" TargetName="Glow" Value="Hidden"/>
<Setter Property="Opacity" TargetName="Shine" Value="0.4"/>
<Setter Property="Background" TargetName="Border" Value="{GetColor(Border.Background)}"/>
</Trigger>
我怎样才能实现这一目标?
有人建议我应该使用ValueConverter:我已经在这里实现了一个:
public class PRBGConverter : IValueConverter {
public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) {
Console.WriteLine( "Clicked!" );
if ( value is SolidColorBrush ) {
SolidColorBrush SCB = value as SolidColorBrush;
SCB.Color = Color.FromArgb( 0xCC, SCB.Color.R, SCB.Color.G, SCB.Color.B );
return SCB;
} else if ( value is GradientBrush ) {
GradientBrush GB = value as GradientBrush;
foreach ( GradientStop GS in GB.GradientStops )
GS.Color = Color.FromArgb( 0xCC, GS.Color.R, GS.Color.G, GS.Color.B );
return GB;
} else return value;
}
public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) {
Console.WriteLine( "UnClicked!" );
if ( value is SolidColorBrush ) {
SolidColorBrush SCB = value as SolidColorBrush;
SCB.Color = Color.FromArgb( 0x99, SCB.Color.R, SCB.Color.G, SCB.Color.B );
return SCB;
} else if ( value is GradientBrush ) {
GradientBrush GB = value as GradientBrush;
foreach ( GradientStop GS in GB.GradientStops )
GS.Color = Color.FromArgb( 0x99, GS.Color.R, GS.Color.G, GS.Color.B );
return GB;
} else
return value;
}
}
不幸的是,正如控制台调试输出所证明的那样,从不调用ConvertBack方法,导致按下的按钮背景保持不透明 - 我该如何纠正?
XAML -
<Trigger Property="IsPressed" Value="True">
<Setter Property="Visibility" TargetName="Glow" Value="Hidden"/>
<Setter Property="Opacity" TargetName="Shine" Value="0.4"/>
<Setter Property="Background" TargetName="Border">
<Setter.Value>
<Binding ElementName="Border" Path="Background" Converter="{StaticResource BGConverter}" UpdateSourceTrigger="PropertyChanged"/>
</Setter.Value>
</Setter>
</Trigger>