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



我们可以将ListView设置为在用户单击ListViewItem后不保留选择吗?有可能做到吗?

注意:这不是关于改变"IsSelected"上的样式,而是实际上在用户点击后没有选择。

我试过以下几种,但都不成功。视图中:

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

在具有视图数据上下文的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();
}
}

我想我知道一种模仿它的方法,把按钮作为项目的ItemsControl。然而,我真的很想知道我是否可以用ListView做到这一点。

更新

我已经编写了以下事件处理程序wrt Clemens&Ed Plunkett的建议。但是我该把代码放在哪里呢?我不应该把它放在VM构造函数中,因为每次SelectedModel都设置为null,这个事件处理程序逻辑也是如此。

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

使用应答更新

我已尝试使用ItemsControl。我编写了ItemsControl的ItemTemplate,它有一个与鼠标单击命令绑定的按钮。这很好。我还编写了ItemsControl的ItemTemplate,我打算定义它的样式,但是ItemContainerStyle只能有ContentPresenter的TargetType。这意味着我不能在样式中添加Border、ScrollViewer、Grid或任何其他UI元素。当然,我可以直接在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>

我发现ItemsControl是原始的,而不是优雅的。

所以我回到ListView。我所做的是:

  1. 我已经为ListView定义了样式,我添加了border、scrollviewer和ItemsPresenter(表示ListViewItem的集合)。

  2. 然后我定义了ListViewItem的样式,它由按钮、命令和样式触发器组成。

  3. 最后,在UserControl/MainWindow中,我添加了ListView。

  4. 我已将ListView Style属性设置为已定义的ListView样式,并将其ItemContainerStyle属性设置为由ListViewItem定义的样式。

我不设置或使用ListView SelectedItem属性。

我对用户点击的响应不是通过监视到SelectedItem的属性绑定,而是通过处理按钮命令绑定。

这更容易,也更优美。

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

以下是两种样式的定义:

<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>

在ListBoxItem样式中将Focusable属性设置为false可能会对您有所帮助:

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

最新更新