WPF Window - Only allow horizontal resize

后端 未结 8 1220
一生所求
一生所求 2021-01-03 23:02

I want to only allow my WPF window to be resized horizontally. How best can I achieve this?

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-03 23:52

    If you have the following requirements: * Width can be user resized (ResizeMode=CanResize) * Height is automatically sized (SizeToContent=Height)

    It won't work for two reasons: * there is no ResizeMode=CanResizeHeight * when the user resizes the window, it will clobber SizeToContent to "Manual"

    A simple hack I use is to constantly force "SizeToContent" back to my desired value.

    
    
    private void LayoutUpdated(object sender, EventArgs e)
    {
        SizeToContent = SizeToContent.Height;
    }
    

    You can also use the ContentRendered event. The PropertyChanged event won't work. This isn't perfect, since the user can still move the cursor vertically, and it will cause some flickering if done quickly.

提交回复
热议问题