清除其他ListView选择

时间:2016-09-14 04:53:48

标签: c# wpf xaml uwp uwp-xaml

如何正确清除其他ListView中的选择?例如,我有4个列表视图堆叠,如果我在listView1中有一个选择,然后我在listView2中选择一个新项目,我希望listView1中的选择消失,并且listview2中的项目被正确选择。注意:列表视图是单选。

<StackPanel>
   <ListView x:Name="listView1" />
   <ListView x:Name="listView2" />
   <ListView x:Name="listView3" />
   <ListView x:Name="listView4" />
</StackPanel>

我第一次尝试:

<StackPanel>
   <ListView x:Name="listView1" SelectionChanged="selectionchanged1" />
   <ListView x:Name="listView2" SelectionChanged="selectionchanged2" />
   <ListView x:Name="listView3" SelectionChanged="selectionchanged3" />
   <ListView x:Name="listView4" SelectionChanged="selectionchanged4" />
</StackPanel>
代码背后的代码:

private void selectionChanged1(object sender, SelectionChangedEventArgs e)
{
    listView2.SelectedItem = null;
    listView3.SelectedItem = null;
    listView4.SelectedItem = null;
}
private void selectionChanged2(object sender, SelectionChangedEventArgs e)
{
    listView1.SelectedItem = null;
    listView3.SelectedItem = null;
    listView4.SelectedItem = null;
}
...

我遇到的问题是,在listView1中选择并单击listView2中的项目后,listView1中的选项将被清除,但listView2中的项目未被选中。

原因是因为(至少)有两个事件被触发:

  1. choosechanged2已解雇,在listview 1,3,4
  2. 中澄清选择
  3. 因为我在listview1中清除选择,所以也会触发selectionchanged1,这会清除listview 2,3,4中的选择
  4. 我认为selectionchanged2会再次被解雇。
  5. 因此我最终没有选择任何名单。
  6. 你有更好的建议吗?

4 个答案:

答案 0 :(得分:2)

当您将ListView的SelectedItem设置为null时,SelectionChanged事件将再次触发,因为ListView的SelectedItem已更改,因此您可以使用标志跳过第二个SelectionChanged偶数调用。

检查以下代码段。我希望它会对你有所帮助。

XAML

<Window x:Class="ListView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ListView"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel x:Name="MyStackPanel">
            <ListView x:Name="listView1" SelectionChanged="listView1_SelectionChanged" SelectionMode="Single">
                <ListViewItem>Item 1</ListViewItem>
                <ListViewItem>Item 2</ListViewItem>
            </ListView>
            <ListView x:Name="listView2" SelectionChanged="listView1_SelectionChanged" SelectionMode="Single">
                <ListViewItem>Item 3</ListViewItem>
                <ListViewItem>Item 4</ListViewItem>
            </ListView>
            <ListView x:Name="listView3" SelectionChanged="listView1_SelectionChanged" SelectionMode="Single">
                <ListViewItem>Item 5</ListViewItem>
                <ListViewItem>Item 6</ListViewItem>
            </ListView>
            <ListView x:Name="listView4" SelectionChanged="listView1_SelectionChanged" SelectionMode="Single">
                <ListViewItem>Item 7</ListViewItem>
                <ListViewItem>Item 8</ListViewItem>
            </ListView>
        </StackPanel>
    </Grid>
</Window>

守则背后

namespace ListView
{
    using System.Windows;
    using System.Windows.Controls;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool skipSelectionChanged;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (skipSelectionChanged)
            {
                skipSelectionChanged = false;

                return;
            }

            var listView = sender as ListView;

            if (listView != null)
            {
                var name = listView.Name;
                for (int i = 0; i < MyStackPanel.Children.Count; i++)
                {
                    var child = MyStackPanel.Children[i] as ListView;
                    if (child.Name != name && child.SelectedItem != null)
                    {
                        skipSelectionChanged = true;
                        child.SelectedItem = null;
                    }
                }
            }
        }
    }
}

答案 1 :(得分:1)

我想您可能会尝试将此 SelectionChanged 事件用于所有 ListViews

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.AddedItems.Count != 0)
    {
        if (listView1 != sender as ListView) listView1.SelectedItem = null;
        if (listView2 != sender as ListView) listView2.SelectedItem = null;
        if (listView3 != sender as ListView) listView3.SelectedItem = null;
        if (listView4 != sender as ListView) listView4.SelectedItem = null;
    }
}

只检查 ListView 中是否有新设置,如果是,则清除所有其他 ListViews

答案 2 :(得分:0)

也许试试这个:将它们全部链接到同一个处理程序,例如:

<StackPanel>
   <ListView x:Name="listView1" SelectionChanged="selectionchanged" />
   <ListView x:Name="listView2" SelectionChanged="selectionchanged" />
   <ListView x:Name="listView3" SelectionChanged="selectionchanged" />
   <ListView x:Name="listView4" SelectionChanged="selectionchanged" />
</StackPanel>

然后:

private void selectionChanged(object sender, SelectionChangedEventArgs e)
{
    string listViewName = ((ListView)sender).Name;
    if (listViewName == "listView1")
    {
        listView2.SelectedItem = null;
        listView3.SelectedItem = null;
        listView4.SelectedItem = null;
    }
    else if (listViewName == "listView3")
    {
        listView1.SelectedItem = null;
        listView2.SelectedItem = null;
        listView4.SelectedItem = null;
    }
    else if (listViewName == "listView4")
    {
        listView1.SelectedItem = null;
        listView2.SelectedItem = null;
        listView3.SelectedItem = null;
    }
    else if (listViewName == "listView2")
    {
        listView1.SelectedItem = null;
        listView3.SelectedItem = null;
        listView4.SelectedItem = null;
    }
}

它很乱且很长(并且未经测试),但请告诉我它是否有效。

答案 3 :(得分:0)

您可以在清除

等其他字段之前添加空检查

对于listView1:

private void selectionChanged1(object sender, SelectionChangedEventArgs e)
{
    if(listView1.SelectedItem == null)
    {
        listView2.SelectedItem = null;
        listView3.SelectedItem = null;
        listView4.SelectedItem = null;
    }
}

同样,你可以为其他人改变它。