WPF向listview项添加更多交互

时间:2017-07-05 12:06:48

标签: c# .net wpf listview

我将listview绑定到使用Caliburn的viewmodel。我的观点代码如下:

<ListView  x:Name="ListView" Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding ProjectsPMod}" Margin="110,0,110,131" HorizontalContentAlignment="Stretch" BorderThickness="0" Height="111" VerticalAlignment="Bottom">
        <interactivity:Interaction.Triggers>
            <interactivity:EventTrigger EventName="SelectionChanged">
                <cal:ActionMessage MethodName="OpenProjectShell">
                    <cal:Parameter Value="{Binding ElementName=ListView, Path=SelectedItem}" />
                </cal:ActionMessage>
            </interactivity:EventTrigger>
        </interactivity:Interaction.Triggers>
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <ContentControl  Content="{StaticResource  Appbar_Suitcase}" />
                        <Label Content="{Binding Name}"/>
                    </StackPanel>

                    <Separator HorizontalAlignment="Stretch" Margin="0, 10, 0, 0"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

现在每行中的每个项目都只显示其名称和公文包图标。如果我想在该行的下方添加一个“编辑”,如果你点击它就会发生一些事情(可能会出现一个弹出窗口),我该怎么做?

2 个答案:

答案 0 :(得分:0)

如果您的意思是&#34;如何为每一行添加一个编辑按钮?&#34;那么你只需要在ItemTemplate中添加一个按钮:

 <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <ContentControl  Content="{StaticResource  Appbar_Suitcase}" />
                    <Label Content="{Binding Name}"/>
                    <!-- Edit button -->
                    <Button Command="{Binding EditCommandOnViewModel}" Content="Edit" />
                </StackPanel>

                <Separator HorizontalAlignment="Stretch" Margin="0, 10, 0, 0"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>

答案 1 :(得分:0)

您可以使用模板将普通Button看作链接。只需将Button添加到StackPanel并将其绑定到命令:

<ListView  x:Name="ListView" Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding ProjectsPMod}" Margin="110,0,110,131" HorizontalContentAlignment="Stretch" BorderThickness="0" Height="111" VerticalAlignment="Bottom">
    <interactivity:Interaction.Triggers>
        <interactivity:EventTrigger EventName="SelectionChanged">
            <cal:ActionMessage MethodName="OpenProjectShell">
                <cal:Parameter Value="{Binding ElementName=ListView, Path=SelectedItem}" />
            </cal:ActionMessage>
        </interactivity:EventTrigger>
    </interactivity:Interaction.Triggers>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <ContentControl  Content="{StaticResource  Appbar_Suitcase}" />
                    <Label Content="{Binding Name}"/>
                </StackPanel>

                <Separator HorizontalAlignment="Stretch" Margin="0, 10, 0, 0"/>

                <Button Margin="0 10 0 0" Content="Link" Cursor="Hand" Command="{Binding YourCommand}">
                    <Button.Template>
                        <ControlTemplate TargetType="Button">
                            <TextBlock TextDecorations="Underline">
                                <ContentPresenter />
                            </TextBlock>
                        </ControlTemplate>
                    </Button.Template>
                    <Button.Style>
                        <Style TargetType="Button">
                            <Setter Property="Foreground" Value="Blue" />
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Foreground" Value="Red" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

您可以使用Button属性控制StackPanelMargin的位置。