wpf listview取消选择项目

时间:2015-08-19 09:58:39

标签: c# wpf listview

我有两个ListView。当用户从TestCommand选择项目时,主要想法是FirstList用户选择TestTestCommandSecondList。在这种情况下,当用户选择FirstList中的项目时,如何从SecondList取消选择项目,并在用户选择SecondList时在FirstList中使用选择。 我尝试将<ei:ChangePropertyAction TargetName="FirstList" PropertyName="SelectedIndex" Value="-1"></ei:ChangePropertyAction>插入<i:EventTrigger EventName="SelectionChanged"/>,但这不是一个好主意。

<Grid>
    <ListView Width="200" Height="200" ItemsSource="{Binding Items}" HorizontalAlignment="Left" Name="FirstList">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding .}">
                </Button>
            </DataTemplate>
        </ListView.ItemTemplate>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding DataContext.TestCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type vm:MainWindow }}}"></i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListView>
    <ListBox Width="200" Height="200" ItemsSource="{Binding Items}" HorizontalAlignment="Right" x:Name="SecondList" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding .}">
                </Button>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding DataContext.TestTestCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type vm:MainWindow }}}"></i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListBox>
</Grid>

namespace WpfApplication129
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    public MainWindow()
    {
        DataContext = new Data();
        InitializeComponent();
    }
}
public class Data
{
    public ICommand TestCommand { get; set; }
    public ICommand TestTestCommand { get; set; }
    public List<string> Items { get; set; }
    public Data()
    {
        TestCommand = new RelayCommand(() => Test());
        TestTestCommand = new RelayCommand(() => TestTest());
        Items = new List<string>();
        Items.Add("first");
    }
    public void Test()
    {
        MessageBox.Show("Running");
    }
    public void TestTest()
    {
        MessageBox.Show("TestRunning");
    }
}
}

0 个答案:

没有答案