开发者

Binding GradientStop works but reports error

开发者 https://www.devze.com 2023-01-19 15:15 出处:网络
The following code binds a GradientStop to the Background.Color property of TemplatedParent. Everything works but I am getting a binding error in the output window:

The following code binds a GradientStop to the Background.Color property of TemplatedParent. Everything works but I am getting a binding error in the output window:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Background.Color; DataItem=null; target element is 'GradientStop' (HashCode=6944299); target property is 'Color' (type 'Color')

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Class="WpfBindingTest.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="100" Height="100">
<Window.Resources>
    <ControlTemplate x:Key="GradientTemplate" TargetType="{x:Type ContentControl}">
        <Border BorderThickness="1" BorderBrush="{TemplateBinding Background开发者_如何学运维}">
            <Border.Background>
                <LinearGradientBrush  EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="{Binding Path=Background.Color, 
                        RelativeSource={RelativeSource TemplatedParent}}"  Offset="1"/>
                    <GradientStop Color="White"  Offset="0"/>
                </LinearGradientBrush>
            </Border.Background>
            <ContentPresenter/>
        </Border>
    </ControlTemplate>
</Window.Resources>

<Grid x:Name="LayoutRoot">
    <ContentControl Background="Green" Template="{StaticResource GradientTemplate}" >
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="X" />
    </ContentControl>
</Grid>
</Window>


I also had the same error in the Visual Studio console output.

A possible explanation and workaround for this is reported here

Basically if you use a Converter that returns a LinearGradientBrush then you don't get the error

The code is something like this

[ValueConversion(typeof(System.Windows.Media.Color), typeof(LinearGradientBrush))]
class GradientConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var brush = new LinearGradientBrush();
        var color = (Color)value;
        brush.StartPoint = new Point(0.5, 0);
        brush.EndPoint = new Point(0.5, 1);

        brush.GradientStops.Add(new GradientStop(Colors.White, 0));
        brush.GradientStops.Add(new GradientStop((Color)value, 1));

        return brush;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

And in the XAML

<Border BorderThickness="1" BorderBrush="{TemplateBinding Background}" Background="{Binding Path=Background.Color, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource gradConv}}">
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号