XAML and binding problem

L

Lloyd Dupont

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......
 
R

Radek Cerny

Go to codeproject.com, and download everything that Josh Smith has provided
for WPF. It is quite well explained and demonstrates a heap of features.
 
L

Laurent Bugnion, MVP

Hi,

Lloyd said:
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>

Try the syntax:

{Binding ElementName=root, Path=StartColor}

You don't say if these properties are dependency properties or standard
CLR properties. In the second case, be aware that your binding will be
one-time only, so if you change the properties in code, the change won't
be propagated to the binding. If you want the change to be propagated,
make dependency properties.

HTH,
Laurent
 
L

Lloyd Dupont

I dont see the difference between your syntax and my syntax (except that you
permutted the order of ElementName and Path, and I tried, just in case, but
it didn't work)

Anyway, I know it's one shoot, I don't care for now, I would like it to
work, at least!
(And yes the value are initialize before the call to InitializeComponent(),
so that allright)


--
Regards,
Lloyd Dupont
NovaMind Software
Mind Mapping at its best
www.nova-mind.com
Laurent Bugnion said:
Hi,

Lloyd said:
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>

Try the syntax:

{Binding ElementName=root, Path=StartColor}

You don't say if these properties are dependency properties or standard
CLR properties. In the second case, be aware that your binding will be
one-time only, so if you change the properties in code, the change won't
be propagated to the binding. If you want the change to be propagated,
make dependency properties.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft.ch
PhotoAlbum: http://www.galasoft.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
L

Laurent Bugnion, MVP

Hi,

Lloyd said:
I dont see the difference between your syntax and my syntax (except that
you permutted the order of ElementName and Path, and I tried, just in
case, but it didn't work)

I just made things explicit, while you left the "Path" implicit. I was
not sure if it could be the problem, apparently not. OK, let's try
something else ;-)

You can debug your bindings like this:
http://geekswithblogs.net/lbugnion/archive/2007/04/02/110622.aspx

(see the chapter "How to find binding errors?")

If that still doesn't help, zip your project and send it to me, I'll
take a look. My email address is genuine.
Anyway, I know it's one shoot, I don't care for now, I would like it to
work, at least!
(And yes the value are initialize before the call to
InitializeComponent(), so that allright)

HTH,
Laurent
 
L

Laurent Bugnion, MVP

Hi,

After asking Microsoft, the "problem" is in fact that you're not
supposed to subclass controls this way, and the symptom in that case was
actually a scope problem.

The problem is related to this post by Microsoft's Kevin Moore:
http://work.j832.com/2007/06/don-subclass-panel-unless-you-making.html

If you intend to subclass a control (in your case, it was a Slider),
then you need to subclass it properly in code behind, and create a XAML
template for it (usually by adding it to generic.xaml). In other cases,
use a UserControl.

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top