lloyd

I'm trying to create a subclass of Slider which slide from one color to the next and has a gradient brush background.
The C# code has 2 new properties: StartColor, EndColor
The XAML code is the following:
==================
<Slider x:Class="TransparencySlider.ColorSlider"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="root">
<Slider.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="{Binding StartColor, ElementName=root}" Offset="0.0"/>
<GradientStop Color="{Binding EndColor, ElementName=root}" Offset="1.0" />
</LinearGradientBrush>
</Slider.Background>
</Slider>
==================
however at runtime the gradient brush is all white and the log message (in the output windows) is: "cannot find source element for the binding", i.e. cannot find 'root', this slider, the control itself.
mmhh... why is that
what should I write
I'm running into a wall......


Re: Windows Presentation Foundation (WPF) XAML and binding problem

arkhivania

Your StartColor and EndColor must be dependency property or implement notifycation with INotifyPropertyChanged interface.




Re: Windows Presentation Foundation (WPF) XAML and binding problem

lloyd

well, if they are dependency the only thing taht will change is that they will be in sync instead of just initialize the value in InitializeComponent().

That is the not the problem I'm facing which is: the binding just don't work!...





Re: Windows Presentation Foundation (WPF) XAML and binding problem

lloyd

in fact I think it's a naming scope issue, but I don't know how to solve it...



Re: Windows Presentation Foundation (WPF) XAML and binding problem

Matt Eland

Try something like {Binding StartColor, RelativeSource={x:RelativeSource AncestorType={x:Type Slider}}}

That should fix the scope issue. I might have gotten syntax wrong, but that approach usually works for me.