wpfuser-controlswpf-positioning

How to position UserControl in the parent canvas


I want to place this UserControl at Canvas.Left="168", Canvas.Top="213".

However, the control appears at a corner. What should I do?

If I put the values at the point of usage for this class, the values are returned as NaN In that case how can I get the correct Left and Top Values?

Usage:

<Canvas x:Name="DesignerCanvas"
        ClipToBounds="True"
        SnapsToDevicePixels="True">
<Gr:BareNode />
</Canvas>

UserControl:

<UserControl x:Class="DiagramDesigner.BareNode"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Grid>
    <ContentControl Width="50"
                  Height="50"
                  Padding="2"    
                  Canvas.Left="168" Canvas.Top="213">
        <Ellipse IsHitTestVisible="False" >
            <Shape.Fill>
                <RadialGradientBrush Center="0.2, 0.2" GradientOrigin="0.2, 0.2" RadiusX="0.8" RadiusY="0.8">
                    <GradientStop Color="LightBlue" Offset="0"/>
                    <GradientStop Color="Blue" Offset="0.9"/>
                </RadialGradientBrush>
            </Shape.Fill>
        </Ellipse>
    </ContentControl>
   </Grid>
</UserControl>

Solution

  • I'm not sure if you tried this or not, but just from looking at the XAML it appears that you are trying to set the position of the user control inside the user control. That won't work. You need to put it where you use the user control

    <Canvas x:Name="DesignerCanvas"
        ClipToBounds="True"
        SnapsToDevicePixels="True">
       <Gr:BareNode Canvas.Left="168" Canvas.Top="213"/>
    </Canvas>
    

    Take the Canvas.Left="168" Canvas.Top="213" part out of the ContentControl declaration of inside the user control.