C# WPF how to set location,width and height of the controls programmatically?

后端 未结 6 1220
慢半拍i
慢半拍i 2021-01-01 23:14

I\'m doing my first WPF application. im having problem whereby when my form is maximized or fullscreen, my controls wont resize and just stay in the same location. only the

相关标签:
6条回答
  • 2021-01-01 23:19

    you should look at all the different layoutpanels in wpf (Grid, DockPanel, StackPanel, Canvas ...)

    so if you set any properties for your Panel, it behaves different if your LayoutPanel change.

    0 讨论(0)
  • 2021-01-01 23:23

    Use some calculations like (control's previous position * layout's new size) / layout's previous size = control's new position

    But the easiest way is to use XAML Use Grid and put columns and rows in it and set the size of columns and rows to * So on layout size change, your controls will reposition in refer to parent's change in size which your grid is child of it. And you could even have auto resizable controls just by setting the controls' margins in columns and rows. Don't forget horizontal and vertical alignments set to stretch.

    0 讨论(0)
  • 2021-01-01 23:24

    For your specific problem, would recommend you to use a DockPanel and place your controls in it. Here's first bing result: WPF Tutorial | Dock Panel

    And as already suggested by flq and blindmeis, do study the layout panels. That will make your life really simpler in WPF.

    0 讨论(0)
  • 2021-01-01 23:25

    To set Width and Height:

    dockpanel1.width = 230;
    dockpanel1.height = 230;
    

    as for location, wpf uses Margin:

    dockpanel1.Margin = new Thickness(0,440,836,40);
    
    0 讨论(0)
  • 2021-01-01 23:32

    It is possible to programmatically move child elements on a Canvas.

    In xaml:

    <Canvas>
        <YourElement Canvas.Top="x" Canvas.Left="y"/>
    </Canvas>
    

    In C#:

    Canvas.SetTop(YourElement, newX);
    Canvas.SetLeft(YourElement, newY);
    
    0 讨论(0)
  • 2021-01-01 23:35

    If you want explicitly position your child items use Canvas panel

    <Canvas>
        <YourElement Canvas.Top="x" Canvas.Left="y"/>
    </Canvas>
    
    0 讨论(0)
提交回复
热议问题