Wpf 如何通过滚动修复datagrid groupheader

Wpf 如何通过滚动修复datagrid groupheader,wpf,datagrid,styles,wpfdatagrid,wpftoolkit,Wpf,Datagrid,Styles,Wpfdatagrid,Wpftoolkit,您好,我想水平对齐我的组标题。我已经成功地这样做了,但现在的问题是,如果我水平滚动,我的组标题也会滚动。如何解决这个问题。我希望我的组标题是固定的,只有我的内容滚动 我的代码如下 Xaml <Window.Resources> <local:Animals x:Key="animals"/> <CollectionViewSource x:Key="cvs" Source="{Binding Source={StaticResourc

您好,我想水平对齐我的组标题。我已经成功地这样做了,但现在的问题是,如果我水平滚动,我的组标题也会滚动。如何解决这个问题。我希望我的组标题是固定的,只有我的内容滚动

我的代码如下

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));
    }
}
公共类动物
{
私有字符串名称;
公共字符串名
{
获取{返回名称;}
设置{name=value;}
}
私人类别;
公共类别
{
获取{返回类别;}
设置{category=value;}
}
公共动物(字符串名称、类别)
{
this.name=名称;
this.category=类别;
}
}
公共枚举类别
{
两栖动物,
熊,
大猫,
犬科动物,
灵长类,
蜘蛛,
}
公营动物
{
私人名单动物主义者;
公众动物学家
{
获取{return animalList;}
}
公共动物()
{
animalList=新列表();
动物学家。添加(新动物(“加利福尼亚蝾螈”,分类。两栖动物));
动物学家。添加(新动物(“大熊猫”,类别。熊));
动物学家。添加(新动物(“郊狼”,类别。犬科动物));
动物学家。添加(新动物(“金丝蜘蛛”,类别。蜘蛛));
动物学家。添加(新动物(“山猫”,类别。灵长类));
动物学家。添加(新动物(“黑熊”,类别。熊));
动物学家。添加(新动物(“美洲虎”,类别。大型猫科动物));
动物学家。添加(新动物(“婆罗洲长臂猿”,类别。灵长类));
动物学家。添加(新动物(“非洲野猫”,类别。大型猫科动物));
动物学家。添加(新动物(“北极狐”,类别。犬科动物));
动物学家。添加(新动物(“番茄蛙”,分类。两栖动物));
动物学家。添加(新动物(“灰熊”,类别。熊));
动物学家。添加(新动物(“野狗”,类别。犬科动物));
动物学家。添加(新动物(“大猩猩”,类别。灵长类));
动物学家。添加(新动物(“绿树蛙”,分类。两栖动物));
动物学家。添加(新动物(“秃头乌卡里”,类别。灵长类));
动物学家。添加(新动物(“北极熊”,类别。熊));
动物学家。添加(新动物(“黑寡妇蜘蛛”,类别。蜘蛛));
动物学家。添加(新动物(“蝙蝠耳狐狸”,类别。犬科动物));
动物学家。添加(新动物(“猎豹”,类别。大型猫科动物));
动物学家。添加(新动物(“猎豹”,分类。蜘蛛));
}
}

请在这方面提供帮助。

在数据网格周围放置一个ScrollViewer。
我还从数据网格中删除了宽度

<Border Margin="30" BorderBrush="Blue" BorderThickness="2" Padding="10">
 <ScrollViewer HorizontalScrollBarVisibility="Auto" 
               VerticalScrollBarVisibility="Auto">
     <DataGrid ItemsSource="{Binding Source={StaticResource cvs}}"         
               ItemTemplate="{StaticResource animalTemplate}" 
               Name="ic" >
     </DataGrid>
   </ScrollViewer>
</Border>

我找到了一个解决方案,这正是我想要的。尽管Zamboni的解决方案暂时帮助我解决了我的问题,但我一直在寻找更好的解决方案。这是讨论,但我分享的代码,任何人感兴趣

<Window.Resources>
    <local:Animals x:Key="animals"/>

    <CollectionViewSource x:Key="cvs" Source="{Binding AnimalList, Source={StaticResource animals}}">
      <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 Name}" Foreground="MediumSeaGreen"/>
    </DataTemplate>

  </Window.Resources>
  <Grid>
    <Controls:DataGrid ItemsSource="{Binding Source={StaticResource cvs}}"             
    ItemTemplate="{StaticResource animalTemplate}" x:Name="ic">
      <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}">
                    <Grid>
                      <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                      </Grid.RowDefinitions>
                      <local:DynamicCanvas HorizontalAlignment="Stretch" Height="{Binding ElementName=items, Path=ActualHeight}" x:Name="myCanvas">
                        <ItemsPresenter x:Name="items" Canvas.Left="{Binding ElementName=frezenBorder, Path=ActualWidth}"/>
                        <Grid Background="White" Height="{Binding ElementName=items, Path=ActualHeight}" VerticalAlignment="Stretch" x:Name="frezenBorder"
                           Canvas.Left="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ScrollViewer}}}">
                          <TextBlock VerticalAlignment="Stretch" MinWidth="100" FontSize="12" FontWeight="Bold" Width="100"
                                Text="{Binding Name}" Margin="5,0,0,0"/>
                        </Grid>
                      </local:DynamicCanvas>
                      <Separator HorizontalAlignment="Stretch" Grid.Row="1" Margin="-1"/>
                    </Grid>
                  </ControlTemplate>
                </Setter.Value>
              </Setter>
            </Style>
          </GroupStyle.ContainerStyle>
        </GroupStyle>
      </Controls:DataGrid.GroupStyle>
    </Controls:DataGrid>
  </Grid>


谢谢。这不正是我想要的……但它成功了。仍然没有办法在保持标题不变的情况下只滚动内容吗?