单击按钮时如何选择列表视图项

时间:2017-01-24 06:42:59

标签: c# wpf listview

在我的listview中,我创建了一个控件模板。在这个模板中,我创建了一个按钮,因此mit listview中的每个项目都有一个按钮。此按钮表示一个链接,用于打开显示的目录。当我单击按钮时,我的程序会按照我的预期打开资源管理器。但是当我单击未选中项目的按钮时,它会打开所选项目的路径。所以我的问题是,当我点击列表视图中的按钮时,如何更改所选项目。

以下是它的样子:

enter image description here

如您所见,选择“R1”,但我点击“R3”的链接。会发生什么,C:\ Temp \ Folder1被打开,因为“R1”仍然是selecteditem。我希望C:\ Temp \ Folder3获得opend。我认为诀窍应该是单击代表链接的按钮时选择“R3”。有谁知道怎么做?

这是我的XAML代码:

<ListView Grid.Row="1" ItemsSource="{Binding MyCollection}" FontSize="20" SelectedItem="{Binding SelectedMember, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="7,10,7,0" x:Name="ListView1" SelectionChanged="ListView1_SelectionChanged">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Header 1" Width="150"/>
                    <GridViewColumn Header="Header 2" Width="120"/>
                    <GridViewColumn Header="Header 3" Width="120"/>
                    <GridViewColumn Header="Header 4" Width="560"/>
                    <GridViewColumn Header="Header 5" Width="100"/>
                </GridView>
            </ListView.View>
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                    <EventSetter Event="MouseDoubleClick" Handler="ListViewItem_MouseDoubleClick"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="150"/>
                                        <ColumnDefinition Width="120"/>
                                        <ColumnDefinition Width="120"/>
                                        <ColumnDefinition Width="560"/>
                                        <ColumnDefinition Width="100"/>
                                    </Grid.ColumnDefinitions>
                                    <ContentPresenter Grid.Column="0" Content="{Binding ID}" HorizontalAlignment="Center"/>
                                    <ContentPresenter Grid.Column="1" Content="{Binding Name}" HorizontalAlignment="Center"/>
                                    <ContentPresenter Grid.Column="2" Content="{Binding Description}" HorizontalAlignment="Center"/>
                                    <Button Grid.Column="3" Content="{Binding Path}" Style="{StaticResource Link}" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.GoToPathCommand }" HorizontalAlignment="Left" Margin="5,0,0,0" IsEnabled="{Binding ButtonEnabled}"/>
                                    <ContentPresenter Grid.Column="4" Content="{Binding Owner}" HorizontalAlignment="Center"/>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="FontWeight" Value="Bold"/>
                            <Setter Property="Foreground" Value="DarkBlue"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>

2 个答案:

答案 0 :(得分:1)

尝试在ListViewItem样式中添加以下触发器

             <Style.Triggers>
                 <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Setter Property="IsSelected" Value="True"/>
                  </Trigger>
             </Style.Triggers>

答案 1 :(得分:1)

Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.GoToPathCommand }"

此命令将跳转到ViewModel的Cmd。我假设Command将检查选择了哪个项目,它将访问它的属性Path

避免此行为,您可以创建一个命令,该命令将接受参数并从Binding设置它:

<Button Content="{Binding Path}"
    Command="{Binding RelativeSource={RelativeSource FindAncestor,
                   AncestorType={x:Type Window}}, Path=DataContext.GoToPathCommand }"
CommandParameter="{Binding Path}"/>

现在必须调整命令本身:

public MyCmd : ICommand
{

    public void Execute(object parameter)
    {
        string path = (string) parameter;
        //Open the path via explorer
    }
}