2013-07-10 67 views
1

我們試圖使用ICollectionView在組合框內實現簡單的項目分組。 CollectionView中使用的分組和排序正常工作。但由ComboBox創建的彈出式菜單列表不能按預期方式運行,因爲滾動條會滾動組而不是項目。WPF分組組合框滾動組不包含項目

例如:如果有2組的25個項目,然後滾動條將有兩個位置/分通過,而不是期望的50

有人能解釋到爲什麼滾動滾動條滾動組而不是組內的項目,以及我們如何改變這種行爲?

視圖模型建立ICollectionView

public ViewModel() 
{ 

CurrenciesView.Filter = CurrencyFilter; 
CurrenciesView.SortDescriptions.Add(new SortDescription("MajorCurrency", ListSortDirection.Descending)); 
CurrenciesView.SortDescriptions.Add(new SortDescription("Code", ListSortDirection.Ascending)); 
CurrenciesView.GroupDescriptions.Add(new PropertyGroupDescription("MajorCurrency", new CurrencyGroupConverter())); 


public ICollectionView CurrenciesView { get { return CollectionViewSource.GetDefaultView(currencies); } } 
    private ObservableCollection<Currency> currencies = new ObservableCollection<Currency>(); 
public ObservableCollection<Currency> Currencies 
{ 
    get { return this.currencies; } 
    set 
    { 
     if (this.currencies != value) 
     { 
      this.currencies = value; 
      this.PropertyChanged(this, new PropertyChangedEventArgs("Currencies")); 
     } 
    } 
} 

XAML用戶控件託管組合框

<UserControl x:Class="FilterView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:vm="clr-namespace:ViewModels"> 
<UserControl.Resources> 
    <ResourceDictionary> 
     <DataTemplate x:Key="CurrencyItem"> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding Code}" FontWeight="ExtraBold"/> 
       <TextBlock Text="{Binding Description}" Margin="10,0,0,0"/> 
      </StackPanel> 
     </DataTemplate> 
     <Style TargetType="ComboBox"> 
      <Setter Property="MinWidth" Value="100"/> 
      <Setter Property="Margin" Value="0,5,0,5"/> 
     </Style> 
     <DataTemplate x:Key="GroupHeader"> 
      <TextBlock Text="{Binding Name}" Padding="3"/> 
     </DataTemplate> 
    </ResourceDictionary> 
</UserControl.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="Auto"/> 
    </Grid.ColumnDefinitions> 
    <Label Grid.Column="0" Grid.Row="0">Currency</Label> 
    <ComboBox Grid.Column="1" Grid.Row="0" Name="Currency" 
         ItemsSource="{Binding Path=CurrenciesView, Mode=OneWay}" 
         ItemTemplate="{StaticResource CurrencyItem}" 
         SelectedValuePath="Code" 
         IsSynchronizedWithCurrentItem="True"> 
     <ComboBox.GroupStyle> 
      <GroupStyle HeaderTemplate="{StaticResource GroupHeader}"/> 
     </ComboBox.GroupStyle> 
    </ComboBox> 
</Grid> 
</UserControl> 

Scroll Position One

Scroll Position Two

回答

4

固定通過爲子ScrollViewer的PART_Popup模板設置CanContentScroll = false。

<UserControl x:Class="FilterView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:vm="clr-namespace:ViewModels"> 
    <UserControl.Resources> 
    <ResourceDictionary> 
     <DataTemplate x:Key="CurrencyItem"> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="{Binding Code}" FontWeight="ExtraBold"/> 
      <TextBlock Text="{Binding Description}" Margin="10,0,0,0"/> 
     </StackPanel> 
     </DataTemplate> 
     <Style TargetType="ComboBox"> 
     <Setter Property="MinWidth" Value="100"/> 
     <Setter Property="Margin" Value="0,5,0,5"/> 
     </Style> 
     <DataTemplate x:Key="GroupHeader"> 
     <TextBlock Text="{Binding Name}" Padding="3"/> 
     </DataTemplate> 
    </ResourceDictionary> 
    </UserControl.Resources> 
    <Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="Auto"/> 
    </Grid.ColumnDefinitions> 
    <Label Grid.Column="0" Grid.Row="0">Currency</Label> 
    <ComboBox Grid.Column="1" Grid.Row="0" Name="Currency" 
       ItemsSource="{Binding Path=CurrenciesView, Mode=OneWay}" 
       ItemTemplate="{StaticResource CurrencyItem}" 
       SelectedValuePath="Code" 
       IsSynchronizedWithCurrentItem="True" 
       **ScrollViewer.HorizontalScrollBarVisibility="Auto" 
       ScrollViewer.VerticalScrollBarVisibility="Auto" 
       ScrollViewer.CanContentScroll="True"**> 
     <ComboBox.GroupStyle> 
     <GroupStyle HeaderTemplate="{StaticResource GroupHeader}"/> 
     </ComboBox.GroupStyle> 
    </ComboBox> 
    </Grid> 
</UserControl> 

或者在組合框衍生控制:

public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 
     Popup popup = GetTemplateChild("PART_Popup") as Popup; 
     if (popup != null) 
     { 
      ScrollViewer scrollViewer = GetVisualChild<ScrollViewer>(popup.Child); 
      if (scrollViewer != null) 
      { 
       scrollViewer.CanContentScroll = false; 
      } 
     } 
    } 
+0

能否請您給的代碼片段,你是如何解決這個問題,我不明白的地方設置CanContentScroll =假。 – bolia

+0

@bolia有點棘手給一個代碼片段,但我會嘗試更新上面的源代碼。 –

+0

目前尚不清楚PART_Popup究竟是什麼 – Unlockedluca

-2
<Combobox ScrollViewer.CanContentScroll="False"></Combobox> 

使用上面的代碼將解決問題。

問候, Ratikanta

+0

HI, 使用上面的代碼它將解決問題。 –

+0

你好代碼是在原來的答案。這是隱藏的 – Drew

+0

這是在另一個答案建議的相同的解決方案,這是更老,upvoted,接受和更詳細。 – lfurini