WPF combobox selectionchanged and triggered the listbox scroll/locate to the selected item cooperately in mvvm

//xaml
<Window x:Class="WpfApp48.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:i="http://schemas.microsoft.com/expression/2010/interactivity"        
        xmlns:local="clr-namespace:WpfApp48"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="TextBlock" x:Key="tbStyle">
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="FontWeight" Value="ExtraBold"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="FontSize" Value="50"/> 
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ComboBox Grid.Row="0" x:Name="cbx" FontSize="30" ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  SelectedIndex="0" DisplayMemberPath="Id" 
                  SelectedItem="{Binding CBXSelectedBook,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding CbxChangeCommand}" CommandParameter="{Binding ElementName=dg}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ComboBox>
        <ListBox Grid.Row="2" x:Name="dg" ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  SelectedIndex="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <GroupBox BorderBrush="Blue" BorderThickness="3" Height="300" Width="1000" >
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="{Binding Id}" Style="{StaticResource tbStyle}" />
                            <TextBlock Grid.Row="1" Text="{Binding Name}" Style="{StaticResource tbStyle}"/>
                            <TextBlock Grid.Row="2" Text="{Binding ISBN}" Style="{StaticResource tbStyle}"/>
                            <TextBlock Grid.Row="3" Text="{Binding Topic}" Style="{StaticResource tbStyle}"/>
                        </Grid>
                    </GroupBox>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>


//xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp48
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = MainVM.GetVMInstance(this);
        }
    }
}

//viewmodel.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;

namespace WpfApp48
{
    public class MainVM : INotifyPropertyChanged
    {
        private static MainVM VMInstance = null;
        private static object instanceLock = new object();
        public static MainVM GetVMInstance(Window winValue)
        {
            lock(instanceLock) 
            {
                if(VMInstance == null) 
                {
                    VMInstance=new MainVM(winValue);
                }
                return VMInstance;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propName)
        {
            var handler = PropertyChanged;
            if (handler!=null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }

        private ObservableCollection<Book> booksCollection;
        public ObservableCollection<Book> BooksCollection
        {
            get
            {
                return booksCollection;
            }
            set
            {
                if (value != booksCollection)
                {
                    booksCollection = value;
                    OnPropertyChanged("BooksCollection");
                }
            }
        }

        private Book cBXSelectedBook;
        public Book CBXSelectedBook
        {
            get
            {
                return cBXSelectedBook;
            }
            set
            {
                if (value != cBXSelectedBook)
                {
                    cBXSelectedBook = value;
                    OnPropertyChanged("CBXSelectedBook");
                }
            }
        }
        
        static MainVM()
        {

        }
        private MainVM(Window winValue)
        {
            WinView = winValue;
            WinView.Loaded += WinView_Loaded;
        }

        private void WinView_Loaded(object sender, RoutedEventArgs e)
        {
            BooksCollection = new ObservableCollection<Book>();
            for(int i=0;i<1000;i++)
            {
                BooksCollection.Add(new Book()
                {
                    Id = $"{i+1}_{Guid.NewGuid()}",
                    Name=$"Name_{Guid.NewGuid()}",
                    ISBN=$"ISBN_{Guid.NewGuid()}",
                    Title=$"Title_{Guid.NewGuid()}",
                    Topic=$"Topic_{Guid.NewGuid()}",
                });
            }
        }

        public Window WinView { get; set; }

        private DelegateCommand cbxChangeCommand;
        public DelegateCommand CbxChangeCommand
        {
            get
            {
                if(cbxChangeCommand == null)
                {
                    cbxChangeCommand = new DelegateCommand(CbxChangeCommandExecuted);
                }
                return cbxChangeCommand;
            }
        }

        private void CbxChangeCommandExecuted(object obj)
        {
            var lbx = obj as ListBox;
            if(lbx!=null && CBXSelectedBook!=null) 
            {
                lbx.ScrollIntoView(CBXSelectedBook);
            }
        }
    }

    public class Book
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string ISBN { get; set;}        
        public string Title { get; set;}
        public string Topic { get; set; }
    }

    public class DelegateCommand : ICommand
    {
        private readonly Action<object> _execute;
        private readonly Predicate<object> _canExecute;

        public event EventHandler CanExecuteChanged;
        public void RaiseCanExecuteChanged() 
        {
            var handler = CanExecuteChanged;
            if(handler!=null)
            {
                handler?.Invoke(this, EventArgs.Empty);
            }
        }

        public DelegateCommand(Action<object> executeValue,Predicate<object> canExecute)
        {
            _execute = executeValue;
            _canExecute = canExecute;
        }

        public DelegateCommand(Action<object> executeValue):this(executeValue, null) 
        {
        }

        public bool CanExecute(object parameter)
        {
            if(_canExecute==null)
            {
                return true;
            }
            return _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }
    }
}

 

 

 

 

 

3Key part

1.Add System.Windows.Interactivity and reference in xaml

 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"  

2.Used the Interaction.Triggers,EventName InvokeCommandAction CommandParamete{Binding ElementName=lbx},the parameter can pass the listbox to viewmodel

 <i:Interaction.Triggers>
     <i:EventTrigger EventName="SelectionChanged">
         <i:InvokeCommandAction Command="{Binding CbxChangeCommand}" CommandParameter="{Binding ElementName=dg}"/>
     </i:EventTrigger>
 </i:Interaction.Triggers>

 

 

3.The scrolltoview() method to locate the selected item

private DelegateCommand cbxChangeCommand;
public DelegateCommand CbxChangeCommand
{
    get
    {
        if(cbxChangeCommand == null)
        {
            cbxChangeCommand = new DelegateCommand(CbxChangeCommandExecuted);
        }
        return cbxChangeCommand;
    }
}

private void CbxChangeCommandExecuted(object obj)
{
    var lbx = obj as ListBox;
    if(lbx!=null && CBXSelectedBook!=null) 
    {
        lbx.ScrollIntoView(CBXSelectedBook);
    }
}

 

posted @ 2024-04-08 20:45  FredGrit  阅读(34)  评论(0编辑  收藏  举报