maandag 2 augustus 2010

Binding a Brush property to a Color

When you try to bind a Color directly to a Brush property, you get the following error in your Visual Studio output window:

System.Windows.Data Error: 'MS.Internal.Data.DynamicValueConverter' converter failed to convert value '#FFFF00FF' (type 'System.Windows.Media.Color'); BindingExpression: Path='LineColor' DataItem=LineVector' (HashCode=38524289); target element is 'System.Windows.Shapes.Line' (Name=''); target property is 'Stroke' (type 'System.Windows.Media.Brush').. System.InvalidOperationException: Can't convert type System.Windows.Media.Color to type System.Windows.Media.Brush.
   at MS.Internal.Data.DefaultValueConverter.Create(Type sourceType, Type targetType, Boolean targetToSource)
   at MS.Internal.Data.DynamicValueConverter.EnsureConverter(Type sourceType, Type targetType)
   at MS.Internal.Data.DynamicValueConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertToTarget(Object value).

The solution is to first wrap the Color in a Brush:

<Line.Stroke>
      <SolidColorBrush Color="{Binding LineColor}"/>
</Line.Stroke>

Geen opmerkingen:

Een reactie posten