C# listview.selectionchanged,我可以在每次点击一个项目时触发它吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1089321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:58:42  来源:igfitidea点击:

listview.selectionchanged, can I make it fire everytime I click an item?

c#wpflistviewselectionchanged

提问by user113164

Is there a way to make the selectionchanged event fire every time a selection in the listview is clicked, instead of only when it changes?

有没有办法在每次单击列表视图中的选择时触发 selectionchanged 事件,而不是仅在它更改时触发?

For example, lets say i have a listview with only one object in it. The user clicks that object, and that object contains information that populates some textboxes below. The user starts changing some of the values in these textboxes (which are not bound to the object). They then decide that they dont want what is in those text boxes so they'd like to reset everything to what is in the object in the listview. But when they click the one object in the listview, nothing happens, because the selection has not changed.

例如,假设我有一个列表视图,其中只有一个对象。用户单击该对象,该对象包含填充下面一些文本框的信息。用户开始更改这些文本框中的某些值(未绑定到对象)。然后他们决定不想要这些文本框中的内容,因此他们希望将所有内容重置为列表视图中对象中的内容。但是当他们单击列表视图中的一个对象时,没有任何反应,因为选择没有改变。

Hope that makes sense. Anyone know how I can get around this?

希望这是有道理的。有谁知道我怎么能解决这个问题?

采纳答案by rmoore

The ListView.SelectionChanged and ListViewItem.Selected events are not going to re-fire if the item is already selected. If you need to re-fire it, you could 'deselect' the item when the event fires.

如果项目已被选中,则 ListView.SelectionChanged 和 ListViewItem.Selected 事件不会重新触发。如果您需要重新触发它,您可以在事件触发时“取消选择”该项目。

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    foreach (var item in e.AddedItems.OfType<ListViewItem>())
    {
        Trace.WriteLine("ListViewItem Selected");
        item.IsSelected = false;
    }
}

Thus allowing you to re-select it ad nauseum. However, if you don't need the actual selection then you should be using an ItemsControl.

从而允许您重新选择它,令人作呕。但是,如果您不需要实际选择,那么您应该使用ItemsControl

If you do want to maintain the select-ability of the item(s) then you should look at registering to a different event than ListView.SelectionChanged, or ListView.Selected. One that works well for this is PreviewMouseDown, as like the initial item selection we want it to occur on both left and right clicks. We could attach it to the single ListViewItem, but since the list may at some point gain more items, we can assign it to all items by using the ItemContainerStyle property of the ListView.

如果您确实希望保持项目的选择能力,那么您应该考虑注册到与 ListView.SelectionChanged 或 ListView.Selected 不同的事件。对此效果很好的是 PreviewMouseDown,就像我们希望它在左键和右键单击时发生的初始项目选择一样。我们可以将它附加到单个 ListViewItem,但由于列表可能在某个时候获得更多项,我们可以使用 ListView 的 ItemContainerStyle 属性将其分配给所有项。

<ListView SelectionChanged="ListView_SelectionChanged">
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <EventSetter Event="PreviewMouseDown"
                         Handler="ListViewItem_PreviewMouseDown" />
        </Style>
    </ListView.ItemContainerStyle>
    <ListViewItem>Item 1</ListViewItem>
    <ListViewItem>Item 2</ListViewItem>
    <ListViewItem>Item 3</ListViewItem>
    <ListViewItem>Item 4</ListViewItem>
</ListView>



private void ListViewItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    Trace.WriteLine("ListViewItem Clicked: " + (sender as ListViewItem).Content);
}

回答by FatemehEbrahimiNik

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
       if(ListView.SelectedIndex != -1)
         {
         //to do staff
         }
       ListView.SelectedIndex = -1;
 }

also we can use this one!

我们也可以使用这个!

 <ListView x:Name="ListView" 
                                  Height="Auto" SelectionChanged="ListView_OnSelectionChanged"
                                  Width="260"
                                  Margin="0,-12,0,-25">
                                    <ListView.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel>
                                                <TextBlock Text="{Binding name_to_show_menu,Mode=TwoWay}" Tapped="UIElement_OnTapped"></TextBlock>
                                            </StackPanel>
                                        </DataTemplate>
                                    </ListView.ItemTemplate>
                                </ListView>

and in code behind

并在后面的代码中

  private void UIElement_OnTapped(object sender, TappedRoutedEventArgs e)
   {   
           //this fire every time 
   }