Tristan
Tristan

Reputation: 96

Execute code based on which button is pressed

Let's say I create a WPF window. Inside that WPF window is a datagrid named "displayTable". I want to execute code based on that table if and only if the user clicks the confirm button. How would I go about this?

In other instances I've been using ICommands, but I need access to the dialog

exampleDialogWindow dialog = new exampleDialogWindow()
{
Topmost = true
};
dialog.ShowDialog();

//Only execute this if the confirm button is pressed
foreach (DataRowView row in dialog.displayTable.SelectedItems)
{
    Console.Out.WriteLine(row["Col1"]);
}

Maybe there's a way to utilize the value returned?

if (dialog.ShowDialog().Value)

Upvotes: 0

Views: 92

Answers (2)

Tristan
Tristan

Reputation: 96

The most simple way I've found to do this is like this

if (dialog.ShowDialog().Value)     
foreach (DataRowView row in dialog.displayTable.SelectedItems)
{
    SubVwr.Tables[0].Dummy.Rows.Add(null, null, null, null, null, row["PID"], row["PN"], row["Description"], row["Revision"], row["Mfr"], row["UOM"], row["PricePer"]);
}

with an button click event for the accept button that looks like this

private void Button_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = true;
    this.Close();
}

Ideally I don't want anything in my xaml.cs , in the future I'd like to find a way to do this through the viewmodel.

Upvotes: 1

Jorge Y.
Jorge Y.

Reputation: 1133

Your ViewModel could have some public events, and your View could suscribe to those. So whenever that button is clicked, your ICommand in the ViewModel launches the event, invoking then the suscribed code in the view, and then you show the dialog from that View code.

Upvotes: 1

Related Questions