我想通过绑定项目的值来改变边框或堆叠面板的背景颜色
<controls:ChildWindow.Resources>
<l:StringToColorConverter x:Key="converter"/>
</controls:ChildWindow.Resources>
...
<DataTemplate>
<StackPanel Orientation="Vertical">
<Border BorderBrush="Black" Background="{Binding Cím, Converter={StaticResource converter}}" x:Name="hatter" BorderThickness="4" CornerRadius="20">
<StackPanel Style="{StaticResource dolgozostack}"
Margin="2,2,2,2">
<TextBlock Text="{Binding Cím}" TextWrapping="Wrap" Foreground="White" FontSize="13" TextAlignment="Center" />
</StackPanel>
</Border>
</StackPanel>
</DataTemplate>
这是我的转换器:
public class StringToColorConverter:IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
if (value.ToString().ToLower() == "Megvalósítás elindítva".ToLower())
return Colors.Green;
else{
SolidColorBrush mybrush=new SolidColorBrush(Colors.Red);
return mybrush;
}
}
}
找到它使用SolidColorBrush
答案 0 :(得分:0)
您可以通过绑定要更改为项目值的Background
属性来执行此操作,并使用将返回给定特定值的特定颜色画笔的转换器。
<TextBlock Text="{Binding Full_Name}" FontSize="16" TextAlignment="Center" TextWrapping="Wrap"
Foreground="{Binding Full_Name, Converter={StaticResource ColorConverter}" />
供参考: