简体   繁体   English

如何获得此DataGridTextColumn的宽度以占用剩余空间的100%?

[英]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: 我尝试了一些不同的方法,包括:

  • Setting the Width attribute to "*". 将“宽度”属性设置为“ *”。 This produced a column that was approximately 10 pixels wide. 这产生了大约10像素宽的列。
  • 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. 将Width属性设置为父列表的ActualWidth绑定。 (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. 奇怪的是,我将此列的文本绑定到相同的值,根据我自己制作窗口的大小,它显示300-1000 +。 The number did not reflect the width of the column, though. 但是,该数字未反映列的宽度。
  • Setting the Width attribute to a hard value. 将Width属性设置为硬值。 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. 这是列的宽度唯一大于Auto属性的时间,但是我宁愿不强制使用特定的宽度。
  • 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" 我将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.

相关问题 如何获取DataGridTextColumn发送者的datagrid父级 - How to get the DataGridTextColumn sender's datagrid parent 如何将工具提示添加到DataGridTextColumn - How do I Add a Tooltip To a DataGridTextColumn 自动调整网格列以占用父节点中的剩余空间 - Autosizing a grid column to take up remaining space in parent 如何获取DataGridTextColumn的默认ControlTemplate? - How can I get the default ControlTemplate of DataGridTextColumn? DataGridTextColumn中的组合框不占据其父级Width - Combobox inside DataGridTextColumn not occupying it's parent Width 如何在DataGridTextColumn上将XAML中的双精度值格式化为德语格式? - How do I format a double value in XAML to German format on a DataGridTextColumn? 如何使用 WPF 中的代码绑定 DataGridTextColumn 的可见性属性? - How do I bind the visibility property of a DataGridTextColumn using code in WPF? 如何使用代码在DataGridTextColumn单元格文本上设置对齐方式 - How do I set alignment on DataGridTextColumn cell text using code 如何使文本框展开以填充网格单元格的剩余空间? - How do i make the textboxes expand to fill the remaining space of the Grid Cell? 如何使DevExpress GridControl列填充网格中的剩余空间? - How do I make a DevExpress GridControl column fill remaining space in the grid?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM