简体   繁体   中英

WPF prevent datagrid auto resize on window resize

I have a datagrid that has its column width set to "Auto". When the datagrid is loaded the columns are the correct size. However, when I change the window width to a size smaller than the datagrid it resizes the columns in order to try and fit everything in the datagrid. When I expand the window size back to full size all the datagrid columns are no longer sized to the content but instead are all set to the same width as if the width is now set to "*".

Is there a way to prevent this from happening? I have tried CanUserResizeColumns="False" and setting the width of the column to a hard coded value.

I am also using materialdesigninxaml, not sure if this is causing this?

That truly is the way it works in WPF. If you know ahead of time what size you want, you should specify it accordingly. So, instead of width="Auto" you specify width="123" .

You could try to keep relative sizing such as this:

width="2*" .... width="*" ... width="10*"

this will ensure that column 1 is twice the size of column 2 and column 3 is 10 times the width of column 2. This, in addition of minWidth and maxWidth should get you where you want to be. Good luck

检查以确保任何宽度为“ *”的列定义

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM