C
C#8mo ago
Shrooms

WPF Help

I have a Wpf window containing the crud operation and a WpfPage containing the datagrid: this is what I've done so far:
No description
3 Replies
Shrooms
ShroomsOP8mo ago
I've created a parent child to link the WPF Window and WPF Page:
public partial class MainWindow : Window
{
private bool IsMaximized = false;

private studentPage studentPageInstance;
private personPage personPageInstance;

public event EventHandler<string> SearchQueryEntered;

public MainWindow()
{
InitializeComponent();
personPageInstance = new personPage();
}

// Helper method to find a child of a specific type in the Visual Tree
private T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
T childItem = FindVisualChild<T>(child);
if (childItem != null)
{
return childItem;
}
}
}
return null;
}

// Person Menu

private void person_click(object sender, RoutedEventArgs e)
{
// Toggle visibility
btnAdd.Visibility = btnAdd.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
btnEdit.Visibility = btnEdit.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
btnDelete.Visibility = btnDelete.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
Main.Content = new personPage();
}

private void PersonPage_EditButton_Click(object sender, RoutedEventArgs e)
{
personPage personPageInstance = FindVisualChild<personPage>(this);

if (personPageInstance != null)
{
personPageInstance.EditBtn_Click(null, null);
}
}
}
public partial class MainWindow : Window
{
private bool IsMaximized = false;

private studentPage studentPageInstance;
private personPage personPageInstance;

public event EventHandler<string> SearchQueryEntered;

public MainWindow()
{
InitializeComponent();
personPageInstance = new personPage();
}

// Helper method to find a child of a specific type in the Visual Tree
private T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
T childItem = FindVisualChild<T>(child);
if (childItem != null)
{
return childItem;
}
}
}
return null;
}

// Person Menu

private void person_click(object sender, RoutedEventArgs e)
{
// Toggle visibility
btnAdd.Visibility = btnAdd.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
btnEdit.Visibility = btnEdit.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
btnDelete.Visibility = btnDelete.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
Main.Content = new personPage();
}

private void PersonPage_EditButton_Click(object sender, RoutedEventArgs e)
{
personPage personPageInstance = FindVisualChild<personPage>(this);

if (personPageInstance != null)
{
personPageInstance.EditBtn_Click(null, null);
}
}
}
I've done this on my EditEntryWindowperson.xaml.cs (it is a popup after clicking the button from the mainwindow)
namespace navigation2
{
public partial class EditEntryWindowPerson : Window
{
private Person? rowDataPerson;
private personPage parentPage;
private int personID;
public Person? UpdatedEntryPerson { get; set; }

public EditEntryWindowPerson(Person data, personPage parentPage, int personID)
{
InitializeComponent();
this.rowDataPerson = data;
this.parentPage = parentPage;
this.personID = personID;

if (rowDataPerson != null)
{
lastNameTextBox.Text = data.lastName;
givenNameTextBox.Text = data.givenName;
middleNameTextBox.Text = data.middleName;
}
}

private void EditBtn_Click(object sender, RoutedEventArgs e)
{
UpdatedEntryPerson = new Person
{
Person_ID = personID,
lastName = lastNameTextBox.Text,
givenName = givenNameTextBox.Text,
middleName = middleNameTextBox.Text
};

Close();
}
}
}
namespace navigation2
{
public partial class EditEntryWindowPerson : Window
{
private Person? rowDataPerson;
private personPage parentPage;
private int personID;
public Person? UpdatedEntryPerson { get; set; }

public EditEntryWindowPerson(Person data, personPage parentPage, int personID)
{
InitializeComponent();
this.rowDataPerson = data;
this.parentPage = parentPage;
this.personID = personID;

if (rowDataPerson != null)
{
lastNameTextBox.Text = data.lastName;
givenNameTextBox.Text = data.givenName;
middleNameTextBox.Text = data.middleName;
}
}

private void EditBtn_Click(object sender, RoutedEventArgs e)
{
UpdatedEntryPerson = new Person
{
Person_ID = personID,
lastName = lastNameTextBox.Text,
givenName = givenNameTextBox.Text,
middleName = middleNameTextBox.Text
};

Close();
}
}
}
Shrooms
ShroomsOP8mo ago
And lastly I've done thins for my personPage.xaml.cs:
Shrooms
ShroomsOP8mo ago
but whenever i click the row and click edit then edit the text it says entry updated succesfully but it is not being edited
Want results from more Discord servers?
Add your server