자신 만의 변환기를 만들어야하는 것 같습니다. 다음은 간단한 시작 예입니다.
public class ColorToSolidColorBrushValueConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
if (null == value) {
return null;
}
// For a more sophisticated converter, check also the targetType and react accordingly..
if (value is Color) {
Color color = (Color)value;
return new SolidColorBrush(color);
}
// You can support here more source types if you wish
// For the example I throw an exception
Type type = value.GetType();
throw new InvalidOperationException("Unsupported type ["+type.Name+"]");
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
// If necessary, here you can convert back. Check if which brush it is (if its one),
// get its Color-value and return it.
throw new NotImplementedException();
}
}
사용하려면 리소스 섹션에서 선언하십시오.
<local:ColorToSolidColorBrushValueConverter x:Key="ColorToSolidColorBrush_ValueConverter"/>
그리고 정적 자원으로 바인딩에 사용이 : 나는 그것을 테스트하지 않았습니다
Fill="{Binding Path=xyz,Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"
. 작동하지 않는다면 의견을 말하십시오.
출처
2010-07-22 14:25:33
HCL
가능한 중복 : http://stackoverflow.com/questions/372693/convert-string-to-brushes-brush-name-in-c –
내가하지 그것이 중복이라고 생각하십시오."Red"와 같은 문자열 값이 아닌 Color 객체에서 변환하려고합니다. 그리고 나는 XAML 변환기로 이것을 구현하고 싶지만, C# 코드로 변환을 수행하는 대신에. – dthrasher