c# - WPF DataGrid wrapped by Grid won't scroll horizontally -
i'm trying make datagrid scroll horizontally when window small display of columns. i'm using grids control positioning of elements. can explain why scrollbar isn't appearing , how can fix it? prefer xaml solution if possible.
here complete code. feel free critique weird see because i'm new wpf.
<window x:class="fblam.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:fblam" mc:ignorable="d" title="mainwindow" width="800" height="600"> <grid> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="*" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="*" /> </grid.columndefinitions> <menu grid.row="0" grid.column="0" ismainmenu="true" height="20" verticalalignment="top"> menu goes here </menu> <grid grid.row="1" grid.column="0" horizontalalignment="center" background="#fff0f0f0"> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="*" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="*" /> <columndefinition width="auto" /> <columndefinition width="*" /> </grid.columndefinitions> <stackpanel grid.column="1" grid.row="0" orientation="horizontal" verticalalignment="stretch" horizontalalignment="stretch"> <button content="button1" horizontalalignment="left" padding="10,2" verticalalignment="center"/> <button content="button2" horizontalalignment="left" padding="10,2" verticalalignment="center" margin="10,0,0,0"/> </stackpanel> <datagrid grid.column="1" grid.row="1" x:name="memberdata" autogeneratecolumns="false" isreadonly="false" canuserresizecolumns="true" canuseraddrows="true" canusersortcolumns="true" itemssource="{binding}" selectionmode="single" headersvisibility="column" horizontalcontentalignment="stretch" verticalcontentalignment="stretch" borderthickness="0" margin="0" gridlinesvisibility="horizontal" scrollviewer.cancontentscroll="true" scrollviewer.horizontalscrollbarvisibility="visible"> <datagrid.columns> <datagridtextcolumn header="id" binding="{binding id}" /> <datagridtextcolumn header="first name" binding="{binding firstname}" /> <datagridtextcolumn header="last name" binding="{binding lastname}" /> <datagridtextcolumn header="school" binding="{binding school}" /> <datagridtextcolumn header="state" binding="{binding state}" /> <datagridhyperlinkcolumn header="email" binding="{binding email}" /> <datagridtextcolumn header="year joined" binding="{binding yearjoined}" /> <datagridtextcolumn header="grade" binding="{binding grade}" /> <datagridcheckboxcolumn header="active" binding="{binding active}" /> <datagridtextcolumn header="amount owed" binding="{binding amountowed, stringformat=c}" /> </datagrid.columns> </datagrid> </grid> </grid>
why have columndefinition
bound actualwidth
of datagrid
? remove that. also, move scrollviewer
properties datagrid
itself.
<window x:class="myproject.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:myproject" mc:ignorable="d" title="mainwindow" width="800" height="600"> <grid> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="*" /> </grid.rowdefinitions> <menu grid.row="0" ismainmenu="true" height="20" verticalalignment="top"> //menu items </menu> <grid grid.row="1" horizontalalignment="stretch" background="#fff0f0f0"> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="*" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="*" /> <columndefinition width="auto" /> <columndefinition width="*" /> </grid.columndefinitions> <stackpanel grid.column="1" grid.row="0" orientation="horizontal" verticalalignment="stretch" horizontalalignment="stretch"> <button content="button1" horizontalalignment="left" padding="10,2" verticalalignment="center"/> <button content="button2" horizontalalignment="left" padding="10,2" verticalalignment="center" margin="10,0,0,0"/> </stackpanel> <datagrid grid.column="1" grid.row="1" autogeneratecolumns="false" isreadonly="false" canuserresizecolumns="true" canuseraddrows="true" canusersortcolumns="true" itemssource="{binding}" selectionmode="single" headersvisibility="column" horizontalcontentalignment="stretch" verticalcontentalignment="stretch" borderthickness="0" margin="0" gridlinesvisibility="horizontal" scrollviewer.cancontentscroll="true" scrollviewer.horizontalscrollbarvisibility="visible" > <datagrid.columns> //10 columns here </datagrid.columns> </datagrid> </grid> </grid> </window>
Comments
Post a Comment