Command Parameter Avalonia

264 Views Asked by At

In my Login view i have a "don't have an account?" button that ,when pressed, opens up a dialog. That dialog holds a Register view.

<dialogHostAvalonia:DialogHost Identifier="NoAccountDialogHost"
                               Grid.ColumnSpan="2"
                               Grid.RowSpan="5"
                             >             
    <dialogHostAvalonia:DialogHost.DataTemplates>
        <DataTemplate DataType="viewmodels:RegisterVM">
            <views:RegisterView />
        </DataTemplate>
    </dialogHostAvalonia:DialogHost.DataTemplates>
</dialogHostAvalonia:DialogHost>

The dialog opens up just fine.

The register view has 3 textboxes bounded to 3 properties in the register viewmodel, which work just fine, and a "REGISTER" button. When pressed, this button should add the user into the database, and then close the dialog. This is how the command looks:

  private void AddAccountToDB(DialogHost dialog)
 {
     //add
     using (var context = new DatabaseContext())
     {
         var user = new User(Utilities.GenerateUserId() , Username!, Password! , Email!);
         context.Credentials.Add(user);
         context.SaveChanges();
     }
     //close dialog
     dialog.CurrentSession!.Close();

 }

As you can see, from the button I am sending, as a parameter, the DialogHost instance. So I did this:

CommandParameter="{RelativeSource AncestorType={x:Type 
dialogHostAvalonia:DialogHost},Mode=FindAncestor}"

Right when I click "REGISTER", an error raises in Program.cs: Error This comes, obviously, from the Command Parameter. I must have done something wrong. Any help would be appreciated!

1

There are 1 best solutions below

0
On

You are providing a RelativeSource as the parameter. You forgot to include the binding so that it would resolve the relative source.

CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type 
dialogHostAvalonia:DialogHost},Mode=FindAncestor}}"