2009-10-09 4 views
3

У меня возникает эта странная проблема с моей группировкой ItemsControl. У меня есть следующая настройка:Элементы WPF не отображаются при подаче группировки

<ItemsControl Margin="3" ItemsSource="{Binding Communications.View}" > 
    <ItemsControl.GroupStyle> 
     <GroupStyle> 
      <GroupStyle.ContainerStyle> 
       <Style TargetType="{x:Type GroupItem}"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="{x:Type GroupItem}"> 
           <Expander> 
            <Expander.Header> 
             <Grid> 
              <Grid.ColumnDefinitions > 
               <ColumnDefinition Width="*" /> 
               <ColumnDefinition Width="Auto" /> 
              </Grid.ColumnDefinitions> 
              <TextBlock Text="{Binding ItemCount, StringFormat='{}[{0}] '}" FontWeight="Bold" /> 
              <TextBlock Grid.Column="1" Text="{Binding Name, Converter={StaticResource GroupingFormatter}, StringFormat='{}Subject: {0}'}" FontWeight="Bold" /> 
             </Grid> 
            </Expander.Header> 
            <ItemsPresenter /> 
           </Expander> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </GroupStyle.ContainerStyle> 
     </GroupStyle> 
    </ItemsControl.GroupStyle> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="Auto" /> 
        <ColumnDefinition Width="*" /> 
        <ColumnDefinition Width="Auto" /> 
       </Grid.ColumnDefinitions> 
       <Grid.RowDefinitions> 
        <RowDefinition /> 
        <RowDefinition /> 
       </Grid.RowDefinitions> 
       <TextBlock FontWeight="Bold" Text="{Binding Inspector, Converter={StaticResource NameFormatter}, StringFormat='{}From {0}:'}" Margin="3" /> 
       <TextBlock Text="{Binding SentDate, StringFormat='{}{0:dd/MM/yy}'}" Grid.Row="1" Margin="3"/> 
       <TextBlock Text="{Binding Message }" Grid.Column="1" Grid.RowSpan="2" Margin="3"/> 
       <Button Command="vm:CommunicationViewModel.DeleteMessageCommand" CommandParameter="{Binding}" HorizontalAlignment="Right" Grid.Column="2">Delete</Button> 
      </Grid> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

В моей модели ViewModel я выставляю CollectionViewSource с именем «Communications». Я исхожу к добавлению группировки туп как так:

Communications.GroupDescriptions.Add(new PropertyGroupDescription("Subject")); 

Теперь проблема я опыт является группировка отлично работает, но я не вижу каких-либо элементов внутри групп. Что я делаю не так? Любые указатели будут высоко оценены.

ответ

1

Я не могу воспроизвести проблему - предположим, вы используете CollectionViewSource? Возможно, это связано с тем, что вы напрямую привязаны к свойству View.

Вот C# код, который я использовал:

public class Communication 
{ 
    public string Subject { get; set; } 
    public string Body { get; set; } 
} 

public partial class Window1 : Window 
{ 
    public Window1() 
    { 
     InitializeComponent(); 

     var source = (CollectionViewSource)Resources["Communications"]; 
     source.Source = new List<Communication>() 
     { 
      new Communication { Subject = "WPF 4.0", Body = "I love what's happening with 4.0"}, 
      new Communication { Subject = "WPF 4.0", Body = "I hear the text rendering is the best feature"}, 
      new Communication { Subject = "Blend 3.0", Body = "Behaviors in Blend 3 change everything"} 
     }; 

     source.GroupDescriptions.Add(new PropertyGroupDescription("Subject")); 
    } 
} 

Вот это XAML - это то же самое, как у вас, но с парой вещей удалены, так как я не ваши конвертеры или команды:

<Window 
    x:Class="GroupStyleDemo.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300" 
    > 
    <Window.Resources> 
     <CollectionViewSource x:Key="Communications" /> 
    </Window.Resources> 
    <Grid> 
     <ItemsControl Margin="3" ItemsSource="{Binding Source={StaticResource Communications}}" > 
      <ItemsControl.GroupStyle> 
       <GroupStyle> 
        <GroupStyle.ContainerStyle> 
         <Style TargetType="{x:Type GroupItem}"> 
          <Setter Property="Template"> 
           <Setter.Value> 
            <ControlTemplate TargetType="{x:Type GroupItem}"> 
             <Expander> 
              <Expander.Header> 
               <Grid> 
                <Grid.ColumnDefinitions > 
                 <ColumnDefinition Width="*" /> 
                 <ColumnDefinition Width="Auto" /> 
                </Grid.ColumnDefinitions> 
                <TextBlock Text="{Binding ItemCount, StringFormat='{}[{0}] '}" FontWeight="Bold" /> 
                <TextBlock Grid.Column="1" Text="{Binding Path=Name}" FontWeight="Bold" /> 
               </Grid> 
              </Expander.Header> 
              <ItemsPresenter /> 
             </Expander> 
            </ControlTemplate> 
           </Setter.Value> 
          </Setter> 
         </Style> 
        </GroupStyle.ContainerStyle> 
       </GroupStyle> 
      </ItemsControl.GroupStyle> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="Auto" /> 
          <ColumnDefinition Width="*" /> 
          <ColumnDefinition Width="Auto" /> 
         </Grid.ColumnDefinitions> 
         <Grid.RowDefinitions> 
          <RowDefinition /> 
          <RowDefinition /> 
         </Grid.RowDefinitions> 
         <TextBlock Text="{Binding Body }" Grid.Column="1" Grid.RowSpan="2" Margin="3"/> 
        </Grid> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 

    </Grid> 
</Window> 
+0

Есть ли способ привязки к ViewModel CollectionViewSource, потому что я не могу ссылаться на XAML resouces из VM. И привязка к CollectionViewSource не работает без привязки непосредственно к .View –

+0

Я только что обнаружил, что из моей библиотеки стилей определяется стиль ItemsControl и переопределяет то, что у меня было в моем коде, поэтому он не работает. Я удалил стиль, и все отлично! Урок выучен. –

Смежные вопросы