DataGrid 的列宽(宽度=“*”)未“刷新”
我得到以下代码,它由一个列出项目的 DataGrid 组成,对应于 ListBox 中的选定项目。一切正常,但 DataGrid 列的宽度表现得很奇怪。我将“名称”列的宽度设置为星号,以便“wert”列占据其内容所需的空间,而“名称”列占据其余空间。这是可行的,但有一点问题。当应用程序启动时,所有列都处于最小宽度,并且只有在列表框中选择另一个项目才会将列的宽度更新为所需的值(“name”表示星号,“wert”表示自动)。 我尝试在应用程序加载时使用数据网格的 UpdateLayout 方法,但这没有帮助。
另外,我正在使用扩展器,当所有扩展器关闭时,我必须首先扩展其中一个项目,然后在列表框中加载另一个项目以使宽度正确。
有人能指出我正确的方向吗? :)
更新:最初的问题解决了,但还有另一个小问题: 柱子不再折叠,但也没有达到所需的尺寸。它们的大小适合其标题名称,而不是行内容。仅当扩展器默认关闭时才会发生这种情况。当我将扩展器设置为扩展时,所有列都根据需要设置为其内容宽度。
这是使用的代码:
<Grid.DataContext>
<XmlDataProvider x:Name="XmlData" Source="entries.xml" XPath="Root/Person" />
</Grid.DataContext>
<ListBox Name="PersonListBox"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource listBoxTemplate}"
IsSynchronizedWithCurrentItem="True"
Visibility="Visible" SelectionMode="Single" SelectedIndex="-1" DataContext="{Binding}">
</ListBox>
<DataGrid IsSynchronizedWithCurrentItem="True" Name="itemGrid"
DataContext="{Binding ElementName=PersonListBox, Path=SelectedItem}"
CanUserResizeColumns="False"
CanUserResizeRows="False"
IsReadOnly="true"
Background="White"
HorizontalScrollBarVisibility="Hidden"
AutoGenerateColumns="False">
<DataGrid.Resources>
<CollectionViewSource x:Key="items" Source="{Binding XPath=item}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="@name"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</DataGrid.Resources>
<DataGrid.ItemsSource>
<Binding Source="{StaticResource items}"/>
</DataGrid.ItemsSource>
<DataGrid.Columns>
<DataGridTextColumn Width="*" Header="Name" Binding="{Binding XPath=@name}"/>
<DataGridTextColumn Header="Wert" Binding="{Binding XPath=@value, Converter={StaticResource sumConverter}}}"/>
</DataGrid.Columns>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander >
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=Items, Converter={StaticResource sumConverter}}" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
I got the following code, it consists of a DataGrid that lists items, corressponding to a selected item in a ListBox. Everything works, but the width of the DataGrid Columns acts wierd. I set the width for the "name" column to star, so that the "wert" column takes the space its contents needs, and the "name" column takes the rest. This is working, but with a little problem. When the application starts, all columns are at their minimal width, and only selecting another item in the ListBox updates the width of the columns to the desired values (star for "name", auto for "wert").
I tried using the UpdateLayout method of the datagrid on application load, but that did not help.
Also, i am using an expander, and when all expanders are closed, i have to first expand one of the items, and then load another item in the listbox for the widths to be correct.
Could anyone please point me in the right direction ? :)
UPDATE: initial question solved, but there is another little problem:
The columns are not collapsed anymore, but theye are not at their desired size either. They are sized to fit their headers name, not the rows content. This only occurs when the expander is closed by default. When i set the expander to be expanded, all columns are set to their contents width, as desired.
Here is the used code :
<Grid.DataContext>
<XmlDataProvider x:Name="XmlData" Source="entries.xml" XPath="Root/Person" />
</Grid.DataContext>
<ListBox Name="PersonListBox"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource listBoxTemplate}"
IsSynchronizedWithCurrentItem="True"
Visibility="Visible" SelectionMode="Single" SelectedIndex="-1" DataContext="{Binding}">
</ListBox>
<DataGrid IsSynchronizedWithCurrentItem="True" Name="itemGrid"
DataContext="{Binding ElementName=PersonListBox, Path=SelectedItem}"
CanUserResizeColumns="False"
CanUserResizeRows="False"
IsReadOnly="true"
Background="White"
HorizontalScrollBarVisibility="Hidden"
AutoGenerateColumns="False">
<DataGrid.Resources>
<CollectionViewSource x:Key="items" Source="{Binding XPath=item}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="@name"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</DataGrid.Resources>
<DataGrid.ItemsSource>
<Binding Source="{StaticResource items}"/>
</DataGrid.ItemsSource>
<DataGrid.Columns>
<DataGridTextColumn Width="*" Header="Name" Binding="{Binding XPath=@name}"/>
<DataGridTextColumn Header="Wert" Binding="{Binding XPath=@value, Converter={StaticResource sumConverter}}}"/>
</DataGrid.Columns>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander >
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=Items, Converter={StaticResource sumConverter}}" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现这个帖子引导我找到答案。总之,默认的 GroupStyle.Panel (StackPanel) 无法适应 DataGrid 宽度“*”,因此列会折叠。只需添加
到您的
GroupStyle
即可。I found this post that led me to the answer. In summary the default
GroupStyle.Panel
(StackPanel) cannot fit the DataGrid Width "*" so the columns collapse. Simply addto your
GroupStyle
and you should be good.