如何修复数据网格组标题的滚动问题
嗨,我正在寻找水平对齐我的组标题。我已经成功地做到了这一点,但现在的问题是,如果我水平滚动,我的组标题也会滚动。如何解决这个问题。我希望我的组标题被修复,并且只有我的组标题被修复。滚动的内容
我的代码如下。
Xaml
<Window.Resources>
<local:Animals x:Key="animals"/>
<CollectionViewSource x:Key="cvs" Source="{Binding Source={StaticResource animals}, Path=AnimalList}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Category" />
<scm:SortDescription PropertyName="Name" />
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Category"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<DataTemplate x:Key="animalTemplate">
<TextBlock Text="{Binding Path=Name}" Foreground="MediumSeaGreen"/>
</DataTemplate>
<Style TargetType="{x:Type HeaderedContentControl}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type HeaderedContentControl}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<ContentPresenter
Content="{TemplateBinding HeaderedContentControl.Header}"
ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}"
ContentSource="Header" VerticalAlignment="Center">
</ContentPresenter>
<ContentPresenter
Content="{TemplateBinding ContentControl.Content}"
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" />
</StackPanel>
<Separator HorizontalAlignment="Stretch" Grid.Row="1" Margin="-1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Border Margin="30" BorderBrush="Blue" BorderThickness="2" Padding="10">
<Controls:DataGrid ItemsSource="{Binding Source={StaticResource cvs}}"
ItemTemplate="{StaticResource animalTemplate}" Name="ic" Width="200" >
<Controls:DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<HeaderedContentControl BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1" Margin="0,0,0,5" >
<HeaderedContentControl.Header>
<TextBlock FontSize="12" FontWeight="Bold" Width="100"
Text="{Binding Name}" Margin="5,0,0,0"/>
</HeaderedContentControl.Header>
<HeaderedContentControl.Content>
<ItemsPresenter/>
</HeaderedContentControl.Content>
</HeaderedContentControl>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</Controls:DataGrid.GroupStyle>
</Controls:DataGrid>
</Border>
代码
public class Animal
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private Category category;
public Category Category
{
get { return category; }
set { category = value; }
}
public Animal(string name, Category category)
{
this.name = name;
this.category = category;
}
}
public enum Category
{
Amphibians,
Bears,
BigCats,
Canines,
Primates,
Spiders,
}
public class Animals
{
private List<Animal> animalList;
public IEnumerable<Animal> AnimalList
{
get { return animalList; }
}
public Animals()
{
animalList = new List<Animal>();
animalList.Add(new Animal("California Newt", Category.Amphibians));
animalList.Add(new Animal("Giant Panda", Category.Bears));
animalList.Add(new Animal("Coyote", Category.Canines));
animalList.Add(new Animal("Golden Silk Spider", Category.Spiders));
animalList.Add(new Animal("Mandrill", Category.Primates));
animalList.Add(new Animal("Black Bear", Category.Bears));
animalList.Add(new Animal("Jaguar", Category.BigCats));
animalList.Add(new Animal("Bornean Gibbon", Category.Primates));
animalList.Add(new Animal("African Wildcat", Category.BigCats));
animalList.Add(new Animal("Arctic Fox", Category.Canines));
animalList.Add(new Animal("Tomato Frog", Category.Amphibians));
animalList.Add(new Animal("Grizzly Bear", Category.Bears));
animalList.Add(new Animal("Dingo", Category.Canines));
animalList.Add(new Animal("Gorilla", Category.Primates));
animalList.Add(new Animal("Green Tree Frog", Category.Amphibians));
animalList.Add(new Animal("Bald Uakari", Category.Primates));
animalList.Add(new Animal("Polar Bear", Category.Bears));
animalList.Add(new Animal("Black Widow Spider", Category.Spiders));
animalList.Add(new Animal("Bat-Eared Fox", Category.Canines));
animalList.Add(new Animal("Cheetah", Category.BigCats));
animalList.Add(new Animal("Cheetah", Category.Spiders));
}
}
请在这方面提供帮助..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经提出了一个正是我正在寻找的解决方案。虽然 Zamboni 的解决方案暂时帮助我解决了我的问题,但我仍在寻找更好的解决方案。讨论这里,但我正在为任何感兴趣的人分享代码。
I have come up with a solution for exactly what i was looking for.Although Zamboni's solution helped me temperorly for solving my problem,i was on the lookout for a better solution. It is discussed here but i am sharing the code for anyone interested.
在 DataGrid 周围放置一个 ScrollViewer。
我还从 DataGrid 中删除了宽度。
Put a ScrollViewer around your DataGrid.
I also removed the Width from the DataGrid.