所以我有一些像这样的XAML:
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Background" Value="Red"></Setter>
</Trigger>
如何将颜色Red
设置为250 200 150
之类的内容?我尝试了Color 250 200 150
和250 200 150
,但无效。有什么想法吗?
答案 0 :(得分:46)
使用HTML样式的颜色。
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Background" Value="#FF0000"></Setter>
</Trigger>
或者,如果你想要alpha透明度:
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Background" Value="#80FF0000"></Setter>
</Trigger>
或使用您的示例颜色250,200,150:
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Background" Value="#FAC896"></Setter>
</Trigger>
答案 1 :(得分:17)
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Background" Value="sc#1.0,0.7,1.0,0.5"></Setter>
</Trigger>
要使用ARGB值,我们必须使用它,稍微不那么直接
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush>
<SolidColorBrush.Color>
<Color A="255" R="250" G="200" B="150"/>
</SolidColorBrush.Color>
</SolidColorBrush>
</Setter.Value>
</Setter>
</Trigger>
<强>更新强>
您还可以使用自定义MarkupExtension
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Background" Value="{markup:BrushFromArgb 255, 250, 200, 150}"/>
</Trigger>
BrushFromArgbExtension
public class BrushFromArgbExtension : MarkupExtension
{
public BrushFromArgbExtension() { }
public BrushFromArgbExtension(byte a, byte r, byte g, byte b)
{
A = a;
R = r;
G = g;
B = b;
}
public byte A { get; set; }
public byte R { get; set; }
public byte G { get; set; }
public byte B { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new SolidColorBrush(Color.FromArgb(A, R, G, B));
}
}
类似的MarkupExtension
也可以用于Color
<SolidColorBrush Color="{markup:FromArgb 255, 255, 200, 150}"/>
FromArgbExtension
public class FromArgbExtension : MarkupExtension
{
public FromArgbExtension() { }
public FromArgbExtension(byte a, byte r, byte g, byte b)
{
A = a;
R = r;
G = g;
B = b;
}
public byte A { get; set; }
public byte R { get; set; }
public byte G { get; set; }
public byte B { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return Color.FromArgb(A, R, G, B);
}
}
答案 2 :(得分:1)
您可以将XAML控件元素上的“ background”属性直接设置为十六进制值。
<Button Background="#292929">
如果愿意,也可以在开头添加Alpha。
<Button Background="#55292929">
了解更多here