2012-07-11 93 views
0

所以我再次来到这里得到一些问题,我无法自己解决他们的问题,所以我的问题是即时得到一个xamlparse执行每个时间的按钮样式加载,在风格里我有一个<矩形>应该得到一个颜色由用户从我的MVVM查询定义。到目前为止,没有任何问题,问题是试图将该颜色值赋予矩形的LinearGradientBrush中的grandientstop。使用的xaml代码:WPF - 在System.Windows.Markup.StaticResourceHolder中指定的值导致了一个异常

<Rectangle x:Name="rectangle" Fill="{Binding Path=StrColor, Converter={StaticResource FadingBrushConverter}, RelativeSource={RelativeSource AncestorType={x:Type Rectangle}}}" HorizontalAlignment="Right" Height="70" Margin="0,0,0,0" RadiusY="5" RadiusX="5" VerticalAlignment="Bottom" Width="70"> 
<Rectangle.Effect> 
    <DropShadowEffect ShadowDepth="1" BlurRadius="8"/> 
</Rectangle.Effect> 
</Rectangle> 

“StrColor”是Color属性。

在我的MVVM我有这个转换器:

Public Class FadingBrushConverter 
     Implements IValueConverter 

    Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert 
     ' TODO: Do some type-checking 
     Dim brush = New LinearGradientBrush() 
     Dim color = DirectCast(value, Color) 
     Dim Bcolor As System.Windows.Media.Color 
     Bcolor.R = 0 
     Bcolor.G = 0 
     Bcolor.B = 0 

     brush.StartPoint = New Point(0.5, 0) 
     brush.EndPoint = New Point(0.5, 1.27) 
     brush.GradientStops.Add(New GradientStop(color, 0)) 
     brush.GradientStops.Add(New GradientStop(Bcolor, 1)) 

     Return brush 

    End Function 

    Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack 
     Throw New NotSupportedException() 
    End Function 
End Class 

我真的不知道我在做什么错在这里,我一直在寻找在互联网上,但到目前为止我解决这个问题没有运气!

我也使用DynamicResource但没有成功!

感谢您的帮助!

+0

请提供更多XAML以更好地理解您的问题。 – 2012-07-11 09:33:28

+0

您确实不需要任何其他xmal来了解问题,反正我找到了解决方法。我创建了一个与转换器一样的只读属性,然后我使用Fill =“{Binding ReadOnlyProperty}”更改了Fill属性,现在它按照我的意愿工作。我真的不知道他为什么不用转换器工作..反正我有问题修复。 – Rui 2012-07-11 09:40:37

回答

0

我认为问题在于绑定。 Rectangle的“Fill”属性绑定到其父级Rectangle的strColor属性(使用"RelativeSource FindAncestor"参数)。我认为假定矩形放置在另一个矩形中是不现实的。即使是某种情况,内置的Rectangle元素也没有strColor属性。如果你想从视图模型的颜色,你应该尝试的东西在排序的:

{Binding Path=StrColor, Converter={StaticResource FadingBrushConverter}" 

当然,这是假设DataContext的设置是否正确。