简体   繁体   中英

How do I get this DataGridTextColumn's width to take up 100% of the remaining space?

I'm having an issue forcing the width of a text column to take up 100% of its remaining space. The DataGrid itself is nested within a ListView, which is further nested in a ListView. It's data binding is an observablelist of obvservablelists, filled with strings of varying lengths.

I'm building in VS 2012, WPF 4.5, with the MVVM-Light toolkit to help with coupling, etc.

The column I'd like to take up 100% of remaining space is the second column (PropertyValue). I've tried a few different things including:

  • Setting the Width attribute to "*". This produced a column that was approximately 10 pixels wide.
  • Setting the Width attribute to "Auto". This produced a column that was, as expected, the minimum required length of the longest string.
  • Setting the Width attribute to the ActualWidth binding of the parent list. (See the code below) This seems to produce the same result as "Auto". Curiously, I bound the text of this column to the same value, and it was showing 300-1000+ depending on what size I made the window itself. The number did not reflect the width of the column, though.
  • Setting the Width attribute to a hard value. This was the only time the width of the column was larger than the Auto property, but I would rather not force a particular width.
  • I tried stripping out everything that I thought was not relevant (including styles and all other elements, and I still end up with the same column widths.

Code is below:

<UserControl x:Class="Foo.Views.XXLViews.XXLInformationView"
    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:ignore="http://www.ignore.com"
    mc:Ignorable="d ignore"
    DataContext="{Binding XXLInformationViewModel, Source={StaticResource Locator}}">
<Grid>
    <Grid Margin="25,10,0,0" HorizontalAlignment="Left">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <StackPanel Orientation="Vertical" Grid.Column="0">
            <ListView ItemsSource="{Binding UnknownPropertiesCollections}" BorderThickness="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Padding="-4,2,0,0" Name="ListOfProperties">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ListView ItemsSource="{Binding}" BorderThickness="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"  Padding="-8,2,0,0">
                            <TextBlock Text="{Binding Name}" Style="{StaticResource PropertyListHeader}"/>
                            <DataGrid ItemsSource="{Binding PropertyCollection}" AutoGenerateColumns="False" Width="Auto">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Width="Auto" Binding="{Binding Path=PropertyName}" FontWeight="Bold">
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Width="{Binding Path=ActualWidth, ElementName=ListOfProperties}" Binding="{Binding Path=PropertyValue}">
                                        <DataGridTextColumn.ElementStyle>
                                            <Style TargetType="TextBlock">
                                                <Setter Property="HorizontalAlignment" Value="Center"/>
                                            </Style>
                                        </DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                </DataGrid.Columns>
                            </DataGrid>
                        </ListView>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackPanel>
    </Grid>
</Grid>

A screen snip of what I am seeing: 在此处输入图片说明

I've spent a bit more time than I expected on this, so hoping that someone can give me a clue as to what the issue is. (And I'll be moving the inline styles for those listviews into a global style once everything is finalized).

I changed HorizontalContentAlignment="Stretch" to HorizontalContentAlignment="Center"

This made the DataGrid only as wide as needed. This was good enough for my situation.

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