2011-01-08 29 views
3

您好我正在尋找水平對齊我的組頭。我已經成功地這樣做,但現在的問題是,如果我水平滾動我的組頭也滾動。如何解決此問題。我希望我的羣組標題是固定的,只有我的內容滾動如何解決從滾動datagrid groupheader

我的代碼如下。

的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)); 
    } 
} 

請在這方面的幫助..

回答

1

將一個ScrollViewer中在你的DataGrid。
我也從DataGrid中刪除了寬度。

<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> 
+0

非常感謝。那不是我正在尋找的東西......但它確實有效。 – biju 2011-01-08 19:34:18

1

我已經想出了正是我一直在尋找for.Although贊博尼的解決方案的解決方案幫助我temperorly解決我的問題,我是在尋找一個更好的解決方案。這是討論here,但我分享代碼感興趣的任何人。

<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>