WPF DataGrid inside Accordion height issue

≯℡__Kan透↙ 提交于 2019-12-23 09:33:03

问题


I am using the latest WPF Toolkit but am running into a height issue when I have a large record set bound into a DataGrid inside an AccordionItem item. The height of the Accordion itself scales nicely but the DataGrid inside the accordion control doesn't get get a ScrollBar or get constrained in any way so the records are hidden.

I know that I am most probably missing something very simple (like a binding from the DataGrid's height property to the Accordion but that seems messy)

here is a cut down version of the code (and yes, this has the same problem if you bind in a big recordset)

<UserControl>
<layouttoolkit:Accordion x:Name="ReportSelector" HorizontalAlignment="Stretch">
    <layouttoolkit:AccordionItem Header="grid 1">
        <dg:DataGrid
         AutoGenerateColumns="False"
         CanUserAddRows="False"
         CanUserDeleteRows="False"
         SelectionMode="Single">
...
            </dg:DataGrid.Columns>
        </dg:DataGrid>

    </layouttoolkit:AccordionItem>
    <layouttoolkit:AccordionItem Header="grid 2">
        <dg:DataGrid
         AutoGenerateColumns="False"
         CanUserAddRows="False"
         CanUserDeleteRows="False"
         SelectionMode="Single">
...
            </dg:DataGrid.Columns>
        </dg:DataGrid>

    </layouttoolkit:AccordionItem>
    <layouttoolkit:AccordionItem Header="grid 3">
        <dg:DataGrid
         AutoGenerateColumns="False"
         CanUserAddRows="False"
         CanUserDeleteRows="False"
         SelectionMode="Single">
...
            </dg:DataGrid.Columns>
        </dg:DataGrid>

    </layouttoolkit:AccordionItem>            
</layouttoolkit:Accordion>
</UserControl>

回答1:


Looks like my initial idea was right - the only way I have been able to solve this one is to bind the MaxHeight of the DataGrid to the ActualHeight of the AccordionItem

Adding the following property to each DataGrid did the trick

MaxHeight="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type layouttoolkit:AccordionItem}},Path=ActualHeight}"



回答2:


I'm so glad I stumbled on this! This QA needs to be upvoted. I had a similar problem except with the column widths.

My DataGrid had Width="Auto", along with some *-sized column widths. Outside of the Accordion the DataGrid rendered fine but inside the Accordion, the width of all of the columns would get squished to 10px each. Couldn't figure out why. Could be a bug?

I noticed that if I set a static Width like 400 instead of Auto, the columns would render properly. Then I tried binding the DataGrid Width to the AccordionItem ActualWidth as you did, and it works perfectly now. Thank you sir!



来源:https://stackoverflow.com/questions/2444871/wpf-datagrid-inside-accordion-height-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!