假设我有一个具有6个属性的对象:
public class MyClass
{
public string Attribute1 { get; set; }
public string Attribute2 { get; set; }
public string Attribute3 { get; set; }
public string Attribute4 { get; set; }
public string Attribute5 { get; set; }
public string Attribute6 { get; set; }
}
我在DataGrid中显示这些对象的集合:
<Grid>
<DataGrid x:Name="myGrid" Margin="5, 5, 5, 5" AutoGenerateColumns="False" CanUserAddRows="False" IsReadOnly="True">
<DataGrid.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Attribute1" Binding="{Binding Attribute1}" Width="5*"/>
<DataGridTextColumn Header="Attribute2" Binding="{Binding Attribute2}" Width="5*"/>
<DataGridTextColumn Header="Attribute3" Binding="{Binding Attribute3}" Width="10*"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
我像这样填充我的网格,按&#34;属性1&#34; ...
进行分组 public MainWindow()
{
InitializeComponent();
ObservableCollection<MyClass> data = new ObservableCollection<MyClass>();
data.Add(new MyClass { Attribute1 = "Red", Attribute4 = "Circle", Attribute5 = "Large", Attribute6 = "Transparent" });
data.Add(new MyClass { Attribute1 = "Red", Attribute4 = "Square", Attribute5 = "Medium", Attribute6 = "Opaque" });
data.Add(new MyClass { Attribute1 = "Red", Attribute4 = "Triangle", Attribute5 = "Large", Attribute6 = "Opaque" });
data.Add(new MyClass { Attribute1 = "Yellow", Attribute4 = "Square", Attribute5 = "Large", Attribute6 = "Transparent" });
data.Add(new MyClass { Attribute1 = "Blue", Attribute4 = "Triangle", Attribute5 = "Small", Attribute6 = "Transparent" });
data.Add(new MyClass { Attribute1 = "Blue", Attribute4 = "Sphere", Attribute5 = "Small", Attribute6 = "Opaque" });
ListCollectionView lcv = new ListCollectionView(data);
lcv.GroupDescriptions.Add(new PropertyGroupDescription("Attribute1"));
myGrid.ItemsSource = lcv;
}
我将GroupItems设计为在扩展器中显示嵌套的DataGrid:
<Window.Resources>
<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander x:Name="exp" IsExpanded="True" Foreground="Black">
<Expander.Header>
<TextBlock Foreground="Black" Text="{Binding Name}"/>
</Expander.Header>
<DockPanel>
<DataGrid ItemsSource="{Binding Items}" x:Name="subGrid" AutoGenerateColumns="False" IsReadOnly="True" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Attribute 4" Binding="{Binding Attribute4}" Width="Auto"/>
<DataGridTextColumn Header="Attribute 5" Binding="{Binding Attribute5}" Width="Auto"/>
<DataGridTextColumn Header="Attribute 6" Binding="{Binding Attribute6}" Width="Auto"/>
</DataGrid.Columns>
</DataGrid>
</DockPanel>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
将我的嵌套DataGrid列宽设置为&#34; Auto&#34; ...效果很好:
问题在于我尝试使用相对宽度......
<DataGrid ItemsSource="{Binding Items}" x:Name="subGrid" AutoGenerateColumns="False" IsReadOnly="True" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Attribute 4" Binding="{Binding Attribute4}" Width="1*"/>
<DataGridTextColumn Header="Attribute 5" Binding="{Binding Attribute5}" Width="1*"/>
<DataGridTextColumn Header="Attribute 6" Binding="{Binding Attribute6}" Width="2*"/>
</DataGrid.Columns>
</DataGrid>
现在看起来像这样......
不仅柱子超级瘦...它们像往常一样不能用鼠标重新调整大小。
这个问题让我感到困惑。我搜索了SO并尝试了几个有点相关的问题而没有任何运气。
如何设置嵌套DataGrid上的列以使用相对宽度?
答案 0 :(得分:3)
我认为问题是WPF不知道DockPanel有多宽,所以你得到那些瘦的列。
一种解决方案是将DockPanel(或DataGrid)的宽度设置为固定宽度,如500像素。
另一种解决方案是将DataGrid的宽度绑定到DockPanel的ActualWidth。这可行,但DataGrid只会增大,当窗口变小时不会缩小。
<DataGrid ItemsSource="{Binding Items}"
x:Name="subGrid"
AutoGenerateColumns="False"
IsReadOnly="True"
CanUserAddRows="False"
Width="{Binding
RelativeSource={RelativeSource AncestorType=DockPanel}, Path=ActualWidth}">
另一种解决方案是将DockPanel的宽度绑定到Expander的ActualWidth。这个问题是它无法正常工作...... Expander变得更大,因为DockPanel变大了我们进入循环。我们真正想要的是Expander的ActualWidth减去足以不使Expander增加它的宽度。我已经尝试了一下,“ActualWidth - 3”似乎有效(但是idk为什么3 ......)。如果添加填充或边距,则可能需要更改此3。为了使它成为一个绑定,我们需要一个IValueConverter。
public class ActualWidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is double)
return (double)value - 3;
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
您需要将转换器添加为资源(应用程序或窗口或其他):
<Window ....
xmlns:app="clr-namespace:WpfApplication25">
<Window.Resources>
<app:ActualWidthConverter x:Key="ActualWidthConverter" />
</Window.Resources>
当然,您需要应用于DockPanel宽度的绑定:
<DockPanel Width="{Binding RelativeSource={RelativeSource AncestorType=Expander}, Path=ActualWidth, Converter={StaticResource ActualWidthConverter}}">
这不是一个完美的解决方案,但它可能会有所帮助吗?此方法的替代版本可以使用MultiBinding;传递Expander的ActualWidth和DockPanel的边距:ActualWidth - Margin.Left - Margin.Right - 3(我还在想为什么3?在其他人的电脑上它也会是3?)。