【问题标题】:List the items in listview according to groups, the data is taken from observable collection根据组列出listview中的项目,数据取自observable collection
【发布时间】:2010-02-19 22:48:25
【问题描述】:

我有一个类存储文件信息,名称列表为 itz observableCollection

导入 System.Collections.ObjectModel

公共类名称列表 继承 ObservableCollection(Of FileInfo)

' Methods
Public Sub New()
    MyBase.Add(New FileInfo("Willa", "Cather", "warning", "err 1"))
    MyBase.Add(New FileInfo("Isak", "Dinesen", "warning", "err 1"))
    MyBase.Add(New FileInfo("Victor", "Hugo", "warning", "err 1"))
    MyBase.Add(New FileInfo("Jules", "Verne", "warning", "err 3"))
    MyBase.Add(New FileInfo("Victor", "Hugo", "warning", "err 1"))
    MyBase.Add(New FileInfo("Jules", "Verne", "warning", "err 3"))
End Sub

结束类

公共类文件信息

Public _name As String
Public _path As String
Public _image As String
Public _errorGrp As String

Public Property ImageIcon() As String
    Get
        Return _image
    End Get
    Set(ByVal value As String)
        _image = value
    End Set
End Property

Public Property PathFile() As String
    Get
        Return _path
    End Get
    Set(ByVal value As String)
        _path = value
    End Set
End Property

Public Property Name() As String
    Get
        Return _name
    End Get
    Set(ByVal value As String)
        _name = value
    End Set
End Property

Public Property ErrorGroup As String
    Get
        Return _errorGrp
    End Get
    Set(ByVal value As String)
        _errorGrp = value
    End Set
End Property

Public Sub New(ByVal fileName As String, ByVal filePath As String, ByVal image As String, ByVal errorGrp As String)
    Name = fileName
    PathFile = filePath
    ImageIcon = image
    ErrorGroup = errorGrp
End Sub

结束类

我需要将此绑定到 xaml listview 项,该项将根据 errorGroup 进行分组 我尝试过的代码是

<Window.Resources >
    <mydata:NameList x:Key="NameListData"/>

  <!--  <DataTemplate x:Key="IconListViewItem" >
        <StackPanel Orientation="Horizontal" >
             <Image Source="/listViewGroupin;component/Images/ViewType.jpg" Height="19" Width="25" ></Image>
            <TextBlock Foreground="DarkBlue" Text="{Binding ImageIcon}"/>
            <TextBlock Foreground="DarkBlue" Text="{Binding Name}"/>
        </StackPanel>
    </DataTemplate>-->

    <CollectionViewSource x:Key='src' 
                      Source="{Binding Source={StaticResource NameListData}}">
        <CollectionViewSource.GroupDescriptions>
           <!-- <PropertyGroupDescription PropertyName="ErrorGroup" />-->
            <PropertyGroupDescription PropertyName="ErrorGroup"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

</Window.Resources>
<Grid>
    <ListView ItemsSource="{Binding Source={StaticResource src}}"
        IsSynchronizedWithCurrentItem="True" >
        <ListView.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}">

                                    <GroupBox BorderBrush="Transparent"  >
                                        <GroupBox.Header >
                                            <StackPanel Orientation="Horizontal" >

                                                <TextBlock Text="{Binding Name}" 
                               Margin="5,0,0,0" Width="100"/>
                                                <Border Height=".2" Width="300" Background="Blue"  ></Border>
                                            </StackPanel>
                                        </GroupBox.Header>
                                    </GroupBox>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>

        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="BorderThickness" Value="1" />
                <Setter Property="Width" Value="Auto" />
                <Setter Property="FontSize" Value="10.4"  />
            </Style>
        </ListView.ItemContainerStyle>

        <ListView.View  >
            <GridView AllowsColumnReorder="False">
                <GridViewColumn Width=" 250" Header="File" DisplayMemberBinding="{Binding Name }">
                </GridViewColumn>
                <GridViewColumn Header="Path" Width="350" DisplayMemberBinding="{Binding PathFile}">
                </GridViewColumn>
                <GridViewColumn Header="Error" Width="350"  DisplayMemberBinding="{Binding ErrorGroup}">
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

它只给我组名和组,而不是项目 请帮帮我

【问题讨论】:

  • 请有人回答我...plzzzzzzzzzz :(

标签: wpf binding


【解决方案1】:

您已将 ListView.GroupStyle 定义为 GroupBox。 GroupBox 是一个 HeaderedContentControl。 您已经定义了 Group Header,但您还需要为项目定义一个占位符 -

<GroupBox.Content>
   <ItemsPresenter />
</GroupBox.Content>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 2021-10-23
    相关资源
    最近更新 更多