简体   繁体   中英

DataGrid columns auto width

I set up DataGrid where should all columns excepts one have size by its content. So I set these columns to auto and the other one to * . But DataGrid ignores it and content in column with header Množ is not completely visible.

<DataGrid Name="dataGrid" Margin="1" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" 
          VerticalAlignment="Stretch" HorizontalAlignment="Stretch" MouseDoubleClick="dataGrid_MouseDoubleClick"
          ItemsSource="{Binding RadkyDokladu.radky}" CanUserSortColumns="False" PreviewKeyDown="dataGrid_PreviewKeyDown"
          AutoGenerateColumns="False" IsReadOnly="True" FontSize="12" SelectionMode="Single" >
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="FontSize" Value="12"/>
        </Style>
    </DataGrid.ColumnHeaderStyle>
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Header="" Binding="{Binding ZASKRTNUTO}" Width="auto" />
        <DataGridTextColumn Header="Náz" Binding="{Binding Artikl_NAME}" MinWidth="10" Width="*"/>
        <DataGridTextColumn Header="Místo" Binding="{Binding Umisteni_NAME}" Width="auto"/>
        <DataGridTextColumn Header="Množ" Binding="{Binding Mnozstvi, StringFormat=F2}" Width="auto">
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="TextBlock">
                    <Setter Property="HorizontalAlignment" Value="Right" />
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header="MJ" Binding="{Binding Jednotka}"  Width="auto"/>
    </DataGrid.Columns>
</DataGrid>

Image after run: 图片

Is something wrong with my code?

It is caused by the size of your DataGrid . By setting:

<DataGrid Margin="1" HorizontalAlignment="Stretch" ...

your DataGrid size belongs to the width of the TreeViews parent (window). You could try activating HorizontalScrollBarVisibility="Visible" to stretch the DataGrids contents and scroll inside your DataGrid .

A second approach is to drop Margin , that the DataGrid can expand depending on contents.

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