【发布时间】:2022-01-19 16:23:30
【问题描述】:
在我的 window.xaml 中,我有以下代码:
xmlns:converters="clr-namespace:HMIPlc.Helpers"
<Window.Resources>
<ResourceDictionary>
<converters:ColorConverter x:Key="ColorOnChange"/>
</ResourceDictionary>
</Window.Resources>
<Rectangle Fill="{Binding Path=varUnit.InSimulation, Converter={StaticResource ColorOnChange}}"/>
我还想给函数一个字符串“黄色”或“橙色”中的值,这样我就可以对不同颜色的不同矩形使用相同的函数。
我在 Helpers 目录中的 ColorConverter.cs 类:
public class ColorConverter : IValueConverter
{
public ColorConverter()
{
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool tempBool = (bool)value;
if(tempBool == true)
{
return new SolidColorBrush(Colors.Orange);
} else
{
return new SolidColorBrush(Colors.White);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
这样我就可以在我的 XAML 中确定颜色是橙色还是黄色。有什么好的方法可以做到吗?
【问题讨论】:
标签: c# wpf xaml data-binding converters