C#C
C#3y ago
Mek

✅ Getting Access To Object For Return Value Avalonia

<ScrollViewer>
  <ItemsControl ItemsSource="{Binding People}">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <Expander VerticalAlignment="Top" Header="{Binding FullName}">
          <StackPanel Orientation="Vertical">
            <Grid ColumnDefinitions="*,*">
              <Button Classes="InnerButton" Grid.Column="0" Content="Edit" Command="{Binding Edit}" CommandParameter="{Binding}" />
              <Button Classes="InnerButton" Grid.Column="1" Content="Delete" Command="{Binding Delete}" CommandParameter="{Binding}" />
            </Grid>
in my
HomeScreenView.axaml
file I have the code above. The ItemsControl is bound to a
List<ContactModel>
to which it iterates through and creates a
DataTemplate
for each contact that is stored in the database therefore inside of each
DataTemplate
is a contact. When the contact is expanded (see image) it comes with 2 buttons. One to edit the contact, and one to delete the contact. I am attempting to get access to the contact that the button click belongs to so that it can be returned. What I mean is that in my axaml file I am binding the
CommandParameter
of each button with
{Binding}
which is the contact's information that belongs to that button. In the
HomeScreenViewModel.cs
file I have
using MeksPhoneBook.Models;
using MeksPhoneBook.Services;
using ReactiveUI;
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Reactive;

namespace MeksPhoneBook.ViewModels;

public class HomeScreenViewModel : ViewModelBase
{
    private Database _db;
    private readonly List<ContactModel> _people;

    public HomeScreenViewModel(Database db)
    {
        _db = db;
        _people = _db.GetContacts();

        NewContact = ReactiveCommand.Create(() => { });
        FAQ = ReactiveCommand.Create(() => { });
        Exit = ReactiveCommand.Create(() => { });

        Edit = ReactiveCommand.Create<ContactModel, ContactModel>(ReturnContact, isTrue);
        Delete = ReactiveCommand.Create<ContactModel, ContactModel>(ReturnContact, isTrue);
    }

    public ContactModel ReturnContact(ContactModel contact)
    {
        return contact;
    }

    public void JoinDiscord()
    {
        string target = "";
        Process.Start(new ProcessStartInfo(target) { UseShellExecute = true });
    }

    public List<ContactModel> People => _people;

    public ReactiveCommand<Unit, Unit> NewContact { get; }
    public ReactiveCommand<Unit, Unit> FAQ { get; }
    public ReactiveCommand<Unit, Unit> Exit { get; }
    public ReactiveCommand<ContactModel, ContactModel> Edit { get; }
    public ReactiveCommand<ContactModel, ContactModel> Delete { get; }
}
I am trying to get access to that contact
{Binding}
so that when the Edit button or the Delete button is clicked, it can send the correct contact and it's information back to the
MainWindowViewModel
for this function
public void ShowHomeScreen(Database db)
{
    var vm = new HomeScreenViewModel(db);

    Observable.Merge(
        vm.NewContact
            .Do(_ => CreateNewUser(db)),
        vm.FAQ
            .Do(_ => ShowFAQ(db)),
        vm.Exit
            .Do(_ => Environment.Exit(0)),
        vm.Edit
            .Do(_ => EditContactScreen(db, contact)),
        vm.Delete
            .Do(_ => _db.DeleteContact(contact))
        )
        .Take(1)
        .Subscribe();
    Content = vm;
}
so that it can pass that contact to where it needs to go. My problem right now is getting access to the desired contact of which button is clicked inside of the Expander so that it can be pushed back, and I am also stuck trying to figure out how to return that contact back to the
MainWindowViewModel
correctly. Thanks
image.png
Was this page helpful?