在列表框中wpf Datagrid列宽度失败 [英] wpf Datagrid columns width fail in ListBox
问题描述
我可以重现用简单的例子(w / o UserControl)。所以如果我添加一个DataGrid作为ListBoxItem,指定列宽度为*将打破列宽度。 BTW,列也不能被用户调整大小。
简单示例:
< Window x:Class =DataGridSpike.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml
Title =MainWindowHeight =350Width =525>
< Grid>
< ListBox HorizontalContentAlignment =Stretch>
<! - 列宽度基本上是可以的,但是由于不是* - >
< DataGrid>
< DataGrid.Columns>
< DataGridTextColumn Header =Column1/>
< DataGridTextColumn Header =Column2/>
< DataGridTextColumn Header =Column3/>
< /DataGrid.Columns>
< / DataGrid>
<! - 断列列宽度: - >
< DataGrid>
< DataGrid.Columns>
< DataGridTextColumn Header =Column1/>
< DataGridTextColumn Header =Column2Width =*/>
< DataGridTextColumn Header =Column3/>
< /DataGrid.Columns>
< / DataGrid>
< / ListBox>
< / Grid>
有谁知道如何修复这个?
Regards
根据sa_ddam213的建议发现一个kludge工作。
我目前还没有其他想法。
我用ListView包装了DataGrid,从ListView中删除了所有的装饰,并将将GridViewColumn 转换为ListView的ActualWidth。也许除ListView之外的一些控件也可以。我正在使用ListView播放。一个简单的边框不起作用。
边距必须调整一些,右侧有一个差距,我不能摆脱(但我可以和它一起生活)除非我禁用DataGrid的HorizontalScrollBarVisibility我在鼠标拖动遗骸宽度时,会得到奇怪的闪烁的水平滚动条。
所以这里是XAML似乎是足够的。
请注意,实际的ListView kludge将是具体UserControl的一部分。
但是,以下(简化)代码显示了我所做的:
< Window x:Class =DataGridSpike .MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006 / xaml
Title =MainWindowHeight =350Width =525>
< Grid>
< ListBox HorizontalContentAlignment =Stretch>
<! - ListView只是一个kludge - >
< ListView HorizontalContentAlignment =StretchPadding = - 4,2,-10,0BorderThickness =0>
< ListView.View>
< GridView>
< GridView.ColumnHeaderContainerStyle>
< Style>
< Setter Property =FrameworkElement.VisibilityValue =Collapsed/>
< / Style>
< /GridView.ColumnHeaderContainerStyle>
< GridViewColumn Width ={Binding Path = ActualWidth,RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type ListView}}}/>
< / GridView>
< /ListView.View>
< DataGrid HorizontalContentAlignment =StretchHorizontalAlignment =Stretch
Padding = - 2,0HorizontalScrollBarVisibility =Disabled>
< DataGrid.Columns>
< DataGridTextColumn Header =Column1/>
< DataGridTextColumn Header =Column2Width =1 */>
< DataGridTextColumn Header =Column3/>
< /DataGrid.Columns>
< / DataGrid>
< / ListView>
< / ListBox>
< / Grid>
< / Window>
仍然,这只是一种解决方法。任何合理的修复仍然欢迎。
I have a ListBox that contains items derived from UserControl. The concrete UserControl contains (among others) a DataGrid. Basically I cannot change the concrete UserControl (changes only for testing purpose; its kind of third party). The DataGrid column width of all columns gets broken as soon as one column width is set to "*", which is required to fill the complete space.
I can reproduce it with a simplified example (w/o UserControl). So if I add a DataGrid as ListBoxItem, specifying a column width of "*" breaks the column width. BTW, the columns also cannot be resized by the user anymore.
Simple example:
<Window x:Class="DataGridSpike.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox HorizontalContentAlignment="Stretch" >
<!-- Column width is basically ok but only since not "*" -->
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="Column1"/>
<DataGridTextColumn Header="Column2"/>
<DataGridTextColumn Header="Column3"/>
</DataGrid.Columns>
</DataGrid>
<!-- Broken column width: -->
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="Column1"/>
<DataGridTextColumn Header="Column2" Width="*"/>
<DataGridTextColumn Header="Column3"/>
</DataGrid.Columns>
</DataGrid>
</ListBox>
</Grid>
Does anyone know how to fix this?
Regards
Based in the suggestion of sa_ddam213 I found a kludge that works. I have no other idea so far.
I wrapped the DataGrid by a ListView, removed all "decoration" from the ListView, and bound the width of the GridViewColumn to the ActualWidth of the ListView. Maybe some control other than ListView works, too. I was just playing around with ListView. A simple border doesn't work.
The margins have to be tweaked some way and there is a gap on the right side I don't get rid of (but I can live with it). Unless I disable HorizontalScrollBarVisibility of the DataGrid I get strange flashing ghost horizontal scrollbars during dragging the widow width by the mouse.
So here is the XAML that seems to be sufficient. Note that the actual ListView kludge will be part of the concrete UserControl. However, the following (simplified) code shows what I did:
<Window x:Class="DataGridSpike.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox HorizontalContentAlignment="Stretch">
<!-- ListView is just a kludge -->
<ListView HorizontalContentAlignment="Stretch" Padding="-4,2,-10,0" BorderThickness="0">
<ListView.View>
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style>
<Setter Property="FrameworkElement.Visibility" Value="Collapsed"/>
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridViewColumn Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}" />
</GridView>
</ListView.View>
<DataGrid HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch"
Padding="-2,0" HorizontalScrollBarVisibility="Disabled">
<DataGrid.Columns>
<DataGridTextColumn Header="Column1"/>
<DataGridTextColumn Header="Column2" Width="1*" />
<DataGridTextColumn Header="Column3" />
</DataGrid.Columns>
</DataGrid>
</ListView>
</ListBox>
</Grid>
</Window>
Still, this is only a workaround. Any reasonable "fix" still welcome.
这篇关于在列表框中wpf Datagrid列宽度失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!