This project has moved. For the latest updates, please go here.

Magnifier zoomfactor binding

Feb 4, 2013 at 1:26 PM
Edited Feb 4, 2013 at 1:31 PM
Likely my lack of understanding binding completely, but why does the zoom factor binding in the code below meet with error 4: Cannot find source for binding with reference 'ElementName=CanvasZoom'. BindingExpression:Path=Value; DataItem=null; target element is 'Magnifier' (Name='magnifier'); target property is 'ZoomFactor' (type 'Double'

The same binding does work on the layouttransform.
<ScrollViewer Name="ViewPort" .... >
  <!-- Canvas must have background. Else it will not receive Mouse events !!!! -->
  <ucs:SizingCanvas x:Name="TrackCanvas" Background="Transparent">
    <Canvas.LayoutTransform>
        <ScaleTransform x:Name="Test" CenterX="0" CenterY="0"
            ScaleX="{Binding ElementName=CanvasZoom,Path=Value}"
            ScaleY="{Binding ElementName=CanvasZoom,Path=Value}">
        </ScaleTransform>
     </Canvas.LayoutTransform>
   </ucs:SizingCanvas>
   <xctk:MagnifierManager.Magnifier>
       <xctk:Magnifier Name="magnifier" Radius="50" 
             ZoomFactor="{Binding ElementName=CanvasZoom,Path=Value}">
        </xctk:Magnifier>
    </xctk:MagnifierManager.Magnifier>
</ScrollViewer>
<Border ..... >
     <Slider Name="CanvasZoom" Value="{Binding PlayingFieldZoom, Source={x:Static properties:Settings.Default}, Mode=TwoWay}" ....... />
</Border>
Developer
Feb 5, 2013 at 12:00 PM
Try with : ZoomFactor="{Binding Source={x:Reference _CanvasZoom}, Path=Value}"
Feb 5, 2013 at 2:13 PM
Here is the problem:
use:
<Slider x:Name="CanvasZoom" ... />
Note the "x:Name" instead of "Name"
Feb 6, 2013 at 7:11 AM
Thanks for answering my question !!!
I've tried both your solutions.
'x:' in front of "Name", seems to make no difference. Same error, same lack of binding. But using "x:" seems better anyway as is has (or may have in future) broader purposes in WPF).
The "Reference" option DOES work at runtime, (regardless of whether I use "Name" or "x:Name"), but meets with a design time error (again, regardless of whether I use x: or not).

So I'm happy that I can resolve my problem with binding and a converter (much less cluttered code). But I still don't understand why "ElementName" does not work, and "Reference" does. And I don't understand why Refernce causes design time errors. I would like to understand......
Feb 6, 2013 at 2:53 PM
Edited Feb 7, 2013 at 5:50 PM
Despite the fact that the Magnifier is declared in your XAML, it is not added to the Visual Tree of your containing parent. It is simply instantiated and added as a value of the "MagnifierManager.Magnifier" attached property on the ScrollViewer. In order for binding with ElementName and RelativeSource to work, the element need to be present in the Visual Tree.

http://stackoverflow.com/questions/12671053/why-does-binding-fail-when-binding-a-child-element-to-another-element-when-the-p
Feb 8, 2013 at 7:18 AM
Ok, emartin, that's interesting. Two questions come tom mind:
1 - Is "MagnifierManager" lacking the plumbing for adding Magnifier into the Visual Tree (after all, Magnifier is a visual element)?
2 - How can I tell (other than through error messages) that an element is part of the Visual Tree or not?

I guess the design time error from x:Reference (Object reference not set to an instance of an object) can be explained by the target object not existing in design time (or can it?). If that's the reason, I'm surprised that Visual Studio shows me the message, as it can see in the XAML file that the object "will" exist in run-time. (That's me nitpicking I guess).
Feb 11, 2013 at 2:08 PM
This issue has been created based on this discussion