microsoft / XamlBehaviors

This is the official home for UWP XAML Behaviors on GitHub.
MIT License
692 stars 110 forks source link

wpf listview's selectionchanged is fired but the command is not fired. #241

Open CodingOctocat opened 1 year ago

CodingOctocat commented 1 year ago

Mini demo:

Win11 + .NET 7.0

动画

<Window x:Class="ListViewSelectionChangedCommandNotFired.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:ListViewSelectionChangedCommandNotFired"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MainWindow"
        Width="800"
        Height="450"
        d:DataContext="{d:DesignInstance Type=local:MainWindow,
                                         IsDesignTimeCreatable=True}"
        DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}"
        FontSize="16"
        Loaded="Window_Loaded"
        mc:Ignorable="d">
    <UniformGrid Rows="1">
        <ListView ItemsSource="{Binding DataList}"
                  SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                  SelectionChanged="ListView_SelectionChanged">
            <b:Interaction.Triggers>
                <b:EventTrigger EventName="SelectionChanged">
                    <b:InvokeCommandAction Command="{Binding SelectionChangedCommand}" />
                </b:EventTrigger>
            </b:Interaction.Triggers>
        </ListView>

        <TextBox x:Name="Output" />
    </UniformGrid>
</Window>
using System.Collections.ObjectModel;
using System.Windows;

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace ListViewSelectionChangedCommandNotFired;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
[INotifyPropertyChanged]
public partial class MainWindow : Window
{
    [ObservableProperty]
    private string? _selectedItem;

    public MainWindow()
    {
        InitializeComponent();
    }

    public ObservableCollection<string> DataList { get; private set; } = new();

    private void ListView_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        Output.Text += "SelectionChanged Event fired!\n";
    }

    [RelayCommand]
    private void SelectionChanged()
    {
        Output.Text += "SelectionChanged Command fired!\n";
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        DataList.Add("Sample data 1");
        SelectedItem = DataList[^1];

        DataList.Add("Sample data 2");
        SelectedItem = DataList[^1];

        DataList.Add("Sample data 3");
        SelectedItem = DataList[^1];
    }
}
michael-hawker commented 10 months ago

@CodingOctocat the Wpf Behaviors are in another repo: https://github.com/microsoft/XamlBehaviorsWpf

@mgoertz-msft maybe should add issue templates here to have a link to point to that repo called out?