简体   繁体   English

选择ListView项目后如何删除ListView选择

[英]How to Remove ListView Selection after Selecting an ListView item

Could we set a ListView to not retain a selection after a user click on a ListViewItem? 我们可以将ListView设置为在用户单击ListViewItem之后不保留选择吗? Could it possibly been done? 能做到吗?

Note: it is not about changing the style on 'IsSelected' but actually to not having the selection after the user click it. 注意:这不是要更改'IsSelected'上的样式,而是实际上是在用户单击它之后没有选择。

I have try the following but it was not successful. 我尝试了以下方法,但未成功。 In the View: 在视图中:

<ListView Grid.Row="1" 
          ItemsSource="{Binding Models}" 
          SelectedItem="{Binding SelectedModel}"
          DisplayMemberPath="DisplayContent"/>

In the ViewModel whom having the View DataContext: 在拥有View DataContext的ViewModel中:

public LookupItemWrapper SelectedModel
{
    get { return _selectionModel; }
    set
    {
        _selectionModel = value;

        OnPropertyChanged();

        if (value != null)
        {
            _eventAggregator.GetEvent<OnRequisitionSelectionInRequisitionProjectNavigationEvent>().Publish(   
                new OnRequisitionSelectionInRequisitionProjectNavigationEventArgs
                {
                    Requisitionid = _selectionModel.Id
                });
        }

        _selectionModel = null;

        OnPropertyChanged();
    }
}

I guess I know a way to imitate that by having ItemsControl with Button as its item. 我想我知道一种通过将ItemsControl和Button作为其项目来模仿它的方法。 However I really like to know if I could do this with a ListView. 但是我真的很想知道是否可以使用ListView做到这一点。

Update 更新资料

I have written the following event handler wrt Clemens & Ed Plunkett suggestion. 我编写了以下事件处理程序,其中包括Clemens和Ed Plunkett的建议。 But where do I place the code? 但是我应该在哪里放置代码? I should not place it inside the VM constructor as everytime SelectedModel is set to null, so does with this event handler logic. 我不应该将其放置在VM构造函数中,因为每当SelectedModel设置为null时,此事件处理程序逻辑都应该这样做。

SelectedModel.PropertyChanged += (s, e) =>
{
    if (SelectedModel != null)
    {
        ((ListView)s).SelectedItem = null;
    }
};

Update With Answer 用答案更新

I have try using ItemsControl. 我尝试使用ItemsControl。 I write the ItemsControl's ItemTemplate which has a Button that bind with a command for mouse click.This work fine. 我写了ItemsControl的ItemTemplate,它具有一个Button,该Button与鼠标单击的命令绑定在一起。 I also write the ItemsControl's ItemTemplate which I intend to define the its style, however ItemContainerStyle can only has TargetType of ContentPresenter. 我还编写了打算定义其样式的ItemsControl的ItemTemplate,但是ItemContainerStyle只能具有ContentPresenter的TargetType。 Meaning I can't add Border, ScrollViewer, Grid or any other UI element inside the style. 这意味着我无法在样式内添加Border,ScrollViewer,Grid或任何其他UI元素。 Sure, I can write it directly on the UserControl/MainWindow such as following: 当然,我可以将其直接写在UserControl / MainWindow上,如下所示:

<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
    <Border BorderBrush="Black" BorderThickness="10">
        <ItemsControl Grid.Column="0" 
                        ItemsSource="{Binding Friends}"
                        ItemContainerStyle="{StaticResource ItemContainerStyle}"
                        ItemTemplate="{StaticResource ItemTemplate}"/>
    </Border>
</ScrollViewer>

I found out ItemsControl is raw and not elegent. 我发现ItemsControl是原始的而不是优雅的。

So I come back to ListView. 所以我回到ListView。 What I have done is: 我所做的是:

  1. I've defined style for the ListView which I add border, scrollviewer and ItemsPresenter (which represent the collection of ListViewItem). 我为ListView定义了样式,我添加了边框,scrollviewer和ItemsPresenter(它们代表ListViewItem的集合)。

  2. Then I've defined style for ListViewItem which consist of a Button, command, style trigger. 然后,我为ListViewItem定义了样式,该样式由Button,命令,样式触发器组成。

  3. Lastly, in the UserControl/MainWindow, I add the ListView. 最后,在UserControl / MainWindow中,添加ListView。

  4. I have set the ListView Style property to the defined ListView style and set its ItemContainerStyle property to the defined style of ListViewItem. 我已经将ListView Style属性设置为定义的ListView样式,并将其ItemContainerStyle属性设置为ListViewItem的定义样式。

I do not set or use ListView SelectedItem property. 我没有设置或使用ListView SelectedItem属性。

I response to user click not by monitoring to the property bind to the SelectedItem rather by handling the button command binding. 我不是通过监视绑定到SelectedItem的属性而是通过处理按钮命令绑定来响应用户单击。

This is easier and much elegent. 这更容易且更优雅。

<ListView Grid.Row="1" 
            ItemsSource="{Binding Models}" 
            ItemContainerStyle="{StaticResource RequisitionNavigationItemListViewItemStyle}" 
            Style="{StaticResource RequisitionNavigationListViewStyle}"/>

Here is both styles definition: 这是两种样式的定义:

<Style x:Key="RequisitionNavigationItemListViewItemStyle" TargetType="{x:Type ListViewItem}">
    <Setter Property="Background" Value="{StaticResource ViewListBackgroundBrush}"/>
    <Setter Property="Foreground" Value="{StaticResource ViewListForegroundBrush}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Button Content="{Binding DisplayContent}"
                        Command="{Binding DataContext.OnSelectingRequisitionCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                        CommandParameter="{Binding}">
                    <Button.Template>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Grid x:Name="grid">
                                <Border x:Name="BorderInButton" Background="{StaticResource RequisitionNavigationBackgroundBrush}"  Height="65" SnapsToDevicePixels="True">
                                    <ContentPresenter x:Name="ContentPresenterInButton" TextBlock.Foreground="{StaticResource RequisitionNavigationForegroundBrush}">
                                        <ContentPresenter.Resources>
                                            <Style TargetType="{x:Type TextBlock}">
                                                <Setter Property="TextWrapping" Value="Wrap"/>
                                                <Setter Property="FontSize" Value="12"/>
                                                <Setter Property="Margin" Value="10"/>
                                            </Style>
                                        </ContentPresenter.Resources>
                                    </ContentPresenter>
                                </Border>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter TargetName="BorderInButton" Property="Background" Value="{StaticResource RequisitionNavigationBackgroundHighlightedBrush}"/>
                                    <Setter TargetName="ContentPresenterInButton" Property="TextBlock.Foreground" Value="{StaticResource RequisitionNavigationForegroundHighlightedBrush}"/>
                                    <Setter Property="Cursor" Value="Hand"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Button.Template>
                </Button>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="RequisitionNavigationListViewStyle" TargetType="{x:Type ListView}">
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListView}">
                <Border x:Name="Bd" BorderBrush="{StaticResource ViewListBorderBrush}" BorderThickness="2 1 4 4" Background="{StaticResource ViewListBackgroundBrush}" Padding="0" SnapsToDevicePixels="True">
                    <ScrollViewer Style="{StaticResource ScrollViewerStyle}">
                        <ItemsPresenter/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Setting Focusable property to false in ListBoxItem style may help you: 在ListBoxItem样式中将Focusable属性设置为false可以帮助您:

<Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}">
  <Setter Property="Focusable" Value="False" />
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM