Reputation: 637
I have two windows one to add users and the other to view all the users, both of them are connected the database. What I want is to show the user automatically in the view all windows after I press the add button in the add users window (before closing it).
In view all window (I am posing the data binding parts only):-
Note:
The following code is in the constructor of MainWindows.xaml.
Basically the ProfileControl
is a custom control, profileList
is
ListBox
and profileCollection
is an ObservableCollection
if (dbObj.GetDbData().Count != 0)
{
foreach (ProfileControl item in dbObj.GetDbData())
{
profileCollection.Add(item);
}
this.profileList.DataContext = profileCollection;
}
The following is for the ListBox.
<ListBox Name="profileList" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<profileControl:ProfileControl Name="pcList" onClickUser="ProfileControl_onClickUser" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
And lastly the following is the add button which is in a AddUser.xaml.
Note: Basically I am checking first that the user I am adding is check-in or not. If not I am adding it to the database.
private void addButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (co.checkIfCheckedIn(memoryCard))
{
MessageBox.Show("The person is inside. ", "No Adding", MessageBoxButton.OK, MessageBoxImage.Stop);
}
else
{
co.AddData(memoryCard, emailTb.Text, contactNumberTb.Text, workPlaceTb.Text, infoTb.Text);
MessageBox.Show("Saving is done. ", "Saved", MessageBoxButton.OK, MessageBoxImage.Asterisk);
this.notificationTB.Text = " Saving is complete.";
}
}
catch (Exception ex)
{
this.notificationTB.Text = ex.Message;
this.notificationTB.Foreground = (Brush)new BrushConverter().ConvertFromString("White");
this.notificationTB.Background = (Brush)new BrushConverter().ConvertFromString("Red");
}
}
I want to add that, it should after I save the data by the add button shows the user in the MainWindows automatically, but it not. And I have to close the application and open it again to view the data. I believe I need to access the ObservableCollection
object profileCollection
in the MainWindows.xaml to do this but I am not sure if it is right and how.
Please advise me, thank.
Upvotes: 0
Views: 54
Reputation: 13364
There is at least 3 solutions from the simplest (bad from a dogmatic "decouple them all" POV but can be pragmatic) to the mass plumbing (good from a design POV):
1) share your collections via a global resource in Application.Current.Resources
:
Application.Current.Resources["MyUserCollection"] = theUserCollection;
2) use a ViewModel shared by both views, exposing the users collection; you'll typically set it as the DataContext
of both views
3) use a message broker to inform the main view that a new user was added, you'll need some MVVM framework for that like MVVM Light or Caliburn Micro
EDIT: here is another solution which is a good compromise:
4) in your MainWindow code-behind expose a Refresh
/Reload
method:
public void ReloadUsers()
{
profileCollection.Clear();
var data = dbObj.GetDbData();
if (data.Count != 0)
{
foreach (ProfileControl item in data)
{
profileCollection.Add(item);
}
this.profileList.DataContext = profileCollection;
}
}
And call it from your other Window:
private void addButton_Click(object sender, RoutedEventArgs e)
{
try
{
...
(Application.Current.MainWindow as MainWindow).ReloadUsers();
}
catch (Exception ex)
{
...
}
}
Upvotes: 1