Hi,@B M-A. Welcome Microsoft Q&A. For selecting Item from the first ListView to the second ListView, and generate a report based on the second ListView and Sum the price, you could refer to the following code.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListView Grid.Column="0" ItemsSource="{Binding View1}" SelectedItem="{Binding LeftRow}"
>
<ListView.View>
<GridView>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}" />
<GridViewColumn Header="Product" DisplayMemberBinding="{Binding Product}" />
<GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}" />
</GridView>
</ListView.View>
</ListView>
<StackPanel Grid.Column="1" Orientation="Vertical" HorizontalAlignment="Center">
<Button Grid.Row="1" Content=">" Command="{Binding MoveToSelectedCommand}" CommandParameter="ToSelected" Margin="10" Width="100"/>
<Button Grid.Row="1" Grid.Column="1" Content="<" Command="{Binding MoveToSourceCommand}" CommandParameter="ToSource" Margin="10" Width="100"/>
<Button Content="generate report ;" Width="50" Command="{Binding GenerateReportCommand}" CommandParameter="{Binding ElementName=SelectedListView}" />
</StackPanel>
<ListView Grid.Column="2" ItemsSource="{Binding View2}" SelectedItem="{Binding RightRow}"
x:Name="SelectedListView">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}" />
<GridViewColumn Header="Product" DisplayMemberBinding="{Binding Product}" />
<GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
Codebedhind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext=new MainViewModel();
}
}
public class MainViewModel : INotifyPropertyChanged
{
private ObservableCollection<Item> sourceItems;
private ObservableCollection<Item> selectedItems;
private CollectionViewSource cvs1 = new();
private CollectionViewSource cvs2 = new();
public ICollectionView View1 { get => cvs1.View; }
public ICollectionView View2 { get => cvs2.View; }
public Item LeftRow { get; set; }
public Item RightRow { get; set; }
public ICommand MoveToSelectedCommand { get; }
public ICommand MoveToSourceCommand { get; }
public ICommand GenerateReportCommand { get; }
public ICommand MyCommand { get; }
public MainViewModel()
{
sourceItems = new ObservableCollection<Item>
{
new Item { Id = 1, Product = "Car", Price = 1000 },
new Item { Id = 2, Product = "Toy", Price = 10 },
new Item { Id = 3, Product = "Apple", Price = 5 }
};
selectedItems = new ObservableCollection<Item>();
cvs1.Source = sourceItems;
cvs2.Source = selectedItems;
MoveToSelectedCommand = new RelayCommand(MoveToSelected);
MoveToSourceCommand = new RelayCommand(MoveToSource);
GenerateReportCommand = new RelayCommand(GenerateReport);
}
private void GenerateReport(object parameter)
{
StringBuilder reportBuilder = new StringBuilder();
decimal totalPrice = 0;
foreach (var item in selectedItems)
{
reportBuilder.AppendLine($"Product: {item.Product}, Price: {item.Price}");
totalPrice += item.Price;
}
reportBuilder.AppendLine($"Total Price: {totalPrice}");
string report = reportBuilder.ToString();
MessageBox.Show(report, "Report");
}
private void MoveToSelected(object parameter)
{
if(parameter.ToString() == "ToSelected")
{
if (LeftRow != null)
{
Item d = LeftRow;
selectedItems.Add(d);
}
}
}
private void MoveToSource(object parameter)
{
if (parameter.ToString() == "ToSource")
{
if (RightRow != null)
{
Item d = RightRow;
selectedItems.Add(d);
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Item
{
public int Id { get; set; }
public string Product { get; set; }
public decimal Price { get; set; }
}
public class RelayCommand : ICommand
{
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.