HorizontalAlignment=Stretch, MaxWidth, and Left aligned at the same time?

前端 未结 8 2387
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 14:47

This seems like it should be easy but I\'m stumped. In WPF, I\'d like a TextBox that stretches to the width of it\'s parent, but only to a maximum width. The problem is that

8条回答
  •  無奈伤痛
    2020-12-04 15:40

    Both answers given worked for the problem I stated -- Thanks!

    In my real application though, I was trying to constrain a panel inside of a ScrollViewer and Kent's method didn't handle that very well for some reason I didn't bother to track down. Basically the controls could expand beyond the MaxWidth setting and defeated my intent.

    Nir's technique worked well and didn't have the problem with the ScrollViewer, though there is one minor thing to watch out for. You want to be sure the right and left margins on the TextBox are set to 0 or they'll get in the way. I also changed the binding to use ViewportWidth instead of ActualWidth to avoid issues when the vertical scrollbar appeared.

提交回复
热议问题