代码之家  ›  专栏  ›  技术社区  ›  Tri Q Tran

应用分组时WPF项不可见

  •  3
  • Tri Q Tran  · 技术社区  · 16 年前

    我的itemscontrol分组有个奇怪的问题。我有以下设置:

    <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中,我公开了一个名为“Communications”的CollectionViewSource。我继续添加分组模式,如下所示:

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

    现在,我所经历的问题是分组工作很好,但是我看不到组内的任何项目。我做错什么了?任何指点都会受到赞赏。

    1 回复  |  直到 16 年前
        1
  •  1
  •   Paul Stovell    16 年前

    我似乎无法重现问题-我假设您使用的是CollectionViewSource?这可能是因为您直接绑定到了视图属性。

    这是我使用的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>