How to stretch in width a WPF user control to its window?

后端 未结 7 1771
执念已碎
执念已碎 2021-01-31 14:52

I have a Window with my user control and I would like to make usercontrol width equals window width. How to do that?

The user control is a horizontal menu and contains a

7条回答
  •  南笙
    南笙 (楼主)
    2021-01-31 15:25

    The Canvas in WPF doesn't provide much automatic layout support. I try to steer clear of them for this reason (HorizontalAlignment and VerticalAlignment don't work as expected), but I got your code to work with these minor modifications (binding the Width and Height of the control to the canvas's ActualWidth/ActualHeight).

    
    
    
        
             
            
        
    
    

    The Canvas is the problem here. If you're not actually utilizing the features the canvas offers in terms of layout or Z-Order "squashing" (think of the flatten command in PhotoShop), I would consider using a control like a Grid instead so you don't end up having to learn the quirks of a control that works differently than you have come to expect with WPF.

提交回复
热议问题