c# - Autosize to only some controls -
i'm working on wpf gui, , want window auto-size content, not everything: have various buttons & other controls, , want autosize width that. if add long item list box, want window stay same size.
example code:
<window x:class="quickiewpf.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" sizetocontent="widthandheight" resizemode="canminimize"> <grid> <grid.rowdefinitions> <rowdefinition/> <rowdefinition/> </grid.rowdefinitions> <stackpanel orientation="horizontal"> <button width="100" content="foo" click="button_click"/> <button width="100" content="bar" click="button_click"/> <button width="100" content="baz" click="button_click"/> </stackpanel> <listbox grid.row="1" name="thelist" height="100"/> </grid> </window> public partial class mainwindow : window { public mainwindow() { initializecomponent(); } private void button_click(object sender, routedeventargs e) { string str = "this long text item wider " + "three buttons. want horizontal scrollbar on listbox, not wider " + "window. want window size buttons, not listbox."; this.thelist.items.add(str); } }
initially, window sized buttons:
after adding long item list box, this:
but i'd rather this:
(i did last screenshot setting maxwidth on list box, isn't solution full application: in full application, it's more 3 buttons; it's buttons, icons, textboxes, labels, etc, , want window autosize whole mess, not listbox @ bottom.)
you can bind width of content not want autosize actual width of content do, example:
<grid> <grid.rowdefinitions> <rowdefinition /> <rowdefinition /> </grid.rowdefinitions> <stackpanel orientation="horizontal" x:name="panel"> <button width="100" content="foo" click="button_click" /> <button width="100" content="bar" click="button_click" /> <button width="100" content="baz" click="button_click" /> </stackpanel> <listbox grid.row="1" name="thelist" height="100" width="{binding elementname=panel, path=actualwidth}" /> </grid>
here bound width of listbox actual width of panel buttons, in case achieves want.
Comments
Post a Comment