[英]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. 我有一个问题,迫使文本列的宽度占用其剩余空间的100%。 The DataGrid itself is nested within a ListView, which is further nested in a ListView. DataGrid本身嵌套在ListView中,而ListView进一步嵌套在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. 我正在VS 2012,WPF 4.5和MVVM-Light工具箱中构建,以帮助进行耦合等。
The column I'd like to take up 100% of remaining space is the second column (PropertyValue). 我想占用100%剩余空间的列是第二列(PropertyValue)。 I've tried a few different things including: 我尝试了一些不同的方法,包括:
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"
我将HorizontalContentAlignment="Stretch"
更改为HorizontalContentAlignment="Center"
This made the DataGrid only as wide as needed. 这使得DataGrid仅根据需要而变宽。 This was good enough for my situation. 这对我的情况已经足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.