简体   繁体   中英

Combobox inside DataGridTextColumn not occupying it's parent Width

<DataGrid AutoGenerateColumns="False" Name="m_gridControl">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="columnCategory" Width="10*">
            <DataGridTextColumn.Header>
                <ComboBox Width="{Binding ElementName=columnCategory, Path=Width}" />
            </DataGridTextColumn.Header>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

With this code, my Combobox doesn't fully occupy my DataGridTextColumn 's width. How do I set the properties to achieve this?

Try replacing

<ComboBox Width="{Binding ElementName=columnCategory, Path=Width}" />

with

<ComboBox Width="{Binding ElementName=columnCategory, Path=ActualWidth}" />

And your problem should be solved.

Width in WPF is an indication while ActualWidth is the real width of the element once it gets rendered.

Edit: To avoid the cropping on the combobox , you need to add a converter (see this answer ) to remove some pixels to it's width. If you need more help on how to do it, here is a sample project I made that demonstrates this.

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