【问题标题】:How do I add a custom item to a ListView with a Collection binding如何使用 Collection 绑定将自定义项添加到 ListView
【发布时间】:2016-04-08 09:16:04
【问题描述】:

我有一个带有项绑定的 ListView,如下所示:

<ListView x:Name="DetailsAZList" ItemsSource="{Binding AZEntries.AZEntries, Mode=OneWay}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border Background="LightGray" BorderBrush="DimGray" BorderThickness="2" CornerRadius="0" Margin="0 2 0 10">
                <Grid MinHeight="140" HorizontalAlignment="Stretch">
                    <StackPanel Margin="10 0" Orientation="Vertical">
                        <TextBlock Text="{Binding ToDoBezeichnung, Mode=OneWay}"/>
                        <!--UserName Unter umständen auch anzeigen, falls Arbeitszeiten anderer User mit aufgeführt werden-->
                        <!--<TextBlock Text="{Binding UserName, Mode=OneWay}"/>-->
                        <TextBlock Text="{Binding start, Mode=OneWay}"/>
                        <TextBlock Text="{Binding finished, Mode=OneWay}"/>
                        <TextBlock Text="{Binding Kostenpflichtig, Mode=OneWay}"/>
                        <controls:WrapPanel Orientation="Horizontal" BlockSize="40">
                            <!-- Example from: stackoverflow.com/questions/28223693/how-to-bind-buttons-in-listview-datatemplate-to-commands-in-viewmodel-mvvmligh -->
                            <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.PlayCommand}" 
                                    CommandParameter="{Binding}" 
                                    Margin="0 0 10 0">
                                <SymbolIcon Symbol="Play"/>
                            </Button>
                            <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.PauseCommand}" 
                                    CommandParameter="{Binding}" 
                                    Margin="0 0 10 0">
                                <SymbolIcon Symbol="Pause"/>
                            </Button>
                            <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.StopCommand}" 
                                    CommandParameter="{Binding}" 
                                    Margin="0 0 10 0">
                                <SymbolIcon Symbol="Stop"/>
                            </Button>
                            <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.EditCommand}" 
                                    CommandParameter="{Binding}" 
                                    Margin="0 0 10 0">
                                <SymbolIcon Symbol="Edit"/>
                            </Button>
                            <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.DeleteCommand}" 
                                    CommandParameter="{Binding}" >
                                <SymbolIcon Symbol="Delete"/>
                            </Button>
                        </controls:WrapPanel>
                    </StackPanel>
                </Grid>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="Margin" Value="0 2 0 0"/>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

我想做的是添加一个最顶层的项目,它要么是未绑定的,要么是不使用数据模板的。像这样:

<ListView x:Name="DetailsAZList" ItemsSource="{Binding AZEntries.AZEntries, Mode=OneWay}" >

    <!-- Here comes the relevant Item -->
    <ListViewItem x:Name="TopmostUnboundItem">
        <Grid HorizontalAlignment="Strech" Background="LightGray">
            <TextBox Text="Add Entry"/>
        </Grid>
    </ListViewItem>
    <!-- Thats it -->

    <ListView.ItemTemplate>
        <DataTemplate>
            <Border Background="LightGray" BorderBrush="DimGray" BorderThickness="2" CornerRadius="0" Margin="0 2 0 10">
                <Grid>...</Grid>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="Margin" Value="0 2 0 0"/>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

这是否可能,而无需将我指定的 Collection 更改为 ObservableCollection 以添加项目,然后使用 TemplateSelector?我希望避免这种情况,因为我觉得这对于一个单独的项目来说有点矫枉过正。

【问题讨论】:

    标签: c# xaml listview win-universal-app


    【解决方案1】:

    使用Header 并将您的内容放入ListView.HeaderTemplate。由于您只想将 1 项放在上面,因此无需更改您的收藏。

    【讨论】:

    • 我应该说,我需要来自 ListViewItem 的视觉和逻辑点击行为。由于 Listview.Header 本身不支持此功能,因此它不是一个选项。现在我的解决方法是使用我放置在 ListView 上方的自定义按钮来执行此操作,目前还可以。我可能会将按钮放在标题中。
    • 你可以把ListViewItem放在Header Template里面。它将拥有您想要从 ListViewItem 获得的所有东西
    • 你有点天才,是吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多