Search code examples
c#wpfcanvasstackpanel

Change StackPanel location on a Canvas programmatically


I have stackpanel in a canvas

The stackpanel has

 <Canvas x:Name="MyCanvas">
            <Slider Template="{StaticResource simpleSlider}"  x:Name="seekBar" Thumb.DragStarted="seekBar_DragStarted" Thumb.DragCompleted="seekBar_DragCompleted" Canvas.Left="347" Canvas.Top="746" Width="900" Height="2" />
            <Rectangle Height="5" />

       <StackPanel Canvas.Left="200" Canvas.Right = "100">
       </StackPanel>
 </Canvas>

At runtime I need to change the location of the objects within the StackPanel.

Ie seekBar.Canvas.Left = 50


Solution

  • The "Canvas.Left" is an example of attached dependency property. The syntax for the C# is:

    Canvas.SetLeft(myStackPanel, 50);
    

    Where myStackPanel is any custom name you must assign using x.Name in the xaml.