2013-03-21 65 views
0

後成爲無形我有ObservableCollection與我想在ListBox顯示項目。 另外,我還爲ListboxItem編寫了一個模板,以便正確顯示我的收藏。 在這個階段,一切正常。與模板WPF列表框項目分組

中的.cs

Sensors = new ObservableCollection<Sensor>(); 
... 
lstBox.ItemsSource = Sensors; 

中的.xaml

... 
<DataTemplate x:Key="SensorTileTemplate"> 
      <Border> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="*"></RowDefinition> 
         <RowDefinition Height="*"></RowDefinition> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="70"></ColumnDefinition> 
         <ColumnDefinition Width="*"></ColumnDefinition> 
         <ColumnDefinition Width="*"></ColumnDefinition> 
        </Grid.ColumnDefinitions> 

        <TextBlock Text="{Binding Name}" Grid.Row="0" Grid.ColumnSpan="3"></TextBlock> 
        <Image Source="{Binding ImageModel.ImgSource}" Style="{StaticResource ImageGlowStyle}" Height="72" Grid.Row="1" Grid.Column="0"></Image> 
        <StackPanel Grid.Row="1" Grid.Column="1" Margin="5"> 
         <TextBlock Text="IP:"></TextBlock> 
         <TextBlock Text="Port:"></TextBlock> 
         <TextBlock Text="Command port:"></TextBlock> 
        </StackPanel> 
        <StackPanel Grid.Row="1" Grid.Column="2" Margin="5"> 
         <TextBlock Text="{Binding DeviceAddress}"></TextBlock> 
         <TextBlock Text="{Binding DeviceDataPort}"></TextBlock> 
         <TextBlock Text="{Binding DeviceControlPort}"></TextBlock> 
        </StackPanel> 
       </Grid> 
      </Border> 
     </DataTemplate> 

<Style x:Key="ContainerStyle"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding IsSelected}" Value="True"> 
        <Setter Property="ListBoxItem.Visibility" Value="Collapsed"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 

... 

<ListBox Name="lstBox" Focusable="False" 
          SelectionChanged="lstBox_SelectionChanged" 
          HorizontalContentAlignment="Stretch" 
          ItemTemplate="{StaticResource SensorTileTemplate}" 
          ItemContainerStyle="{StaticResource ContainerStyle}"> 
       </ListBox> 

的問題,當我需要使用膨脹機作爲一組容器組的某些項目出現。

中的.cs

... 
ICollectionView view = CollectionViewSource.GetDefaultView(Sensors); 
view.GroupDescriptions.Add(new PropertyGroupDescription("GroupNumber")); 

lstBox.ItemsSource = view; 
... 

中的.xaml

<!--Same Template and Style--> 
... 
... 
<Style x:Key="GroupContainerStyle" TargetType="{x:Type GroupItem}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type GroupItem}"> 
         <Expander IsExpanded="True">   
          <Expander.Header> 
           <StackPanel Orientation="Horizontal"> 
            <TextBlock Text="Group #" /> 
            <TextBlock Text="{Binding Name}" /> 
           </StackPanel> 
          </Expander.Header> 
          <Expander.Content> 
           <ItemsPresenter /> 
          </Expander.Content> 
         </Expander> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
... 
<ListBox Name="lstBox" Focusable="False" 
          SelectionChanged="lstBox_SelectionChanged" 
          HorizontalContentAlignment="Stretch" 
          ItemTemplate="{StaticResource SensorTileTemplate}" 
          ItemContainerStyle="{StaticResource ContainerStyle}"> 
        <ListBox.GroupStyle> 
         <GroupStyle ContainerStyle="{StaticResource GroupContainerStyle}" /> 
        </ListBox.GroupStyle> 
       </ListBox> 

此代碼的工作和組項目,但項目不可見。
所以沒有分組項目正確顯示,但與分組擴展沒有顯示任何內容。
我認爲在Expander一些關於ItemsPresenter但無法弄清楚什麼。

回答

1

問題出在我在我的應用程序中使用的第三方主題中。該主題有ListBox模板,如:

<Style TargetType="{x:Type ListBox}"> 
... 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ListBox}"> 
        <Grid> 
         <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2" Background="{DynamicResource ControlBackgroundBrush}" /> 
         <ScrollViewer Margin="1" Style="{DynamicResource NuclearScrollViewer}" Focusable="false" Background="{x:Null}"> 
          <StackPanel Margin="1,1,1,1" IsItemsHost="true" /> 
         </ScrollViewer> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border" /> 
          <Setter Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" TargetName="Border" /> 
         </Trigger> 
         <Trigger Property="IsGrouping" Value="true"> 
          <Setter Property="ScrollViewer.CanContentScroll" Value="false" /> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

所以我用一個ItemsPresenter,而不是在模板中的StackPanel和一切正常了。