Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
Hello all
 
I've got a userControl that houses other userControls within tab items. They are made visible and cleared by interaction with various checkbox's on my parent userControl. I've validation and writetodatabase functions in place which works for parent userControl. What I need to have happen is when "Update" (utilizes CommandBinding) button on uC1 is clicked, that all relevant uC data on other tabs are saved to db as well. I've got validation to work but am stumped in saving the data of uC2/uC3/uC4 etc. ... any pointers would be most welcome.
PS I'm trying to keep my uC's as loosely coupled as possible... as well as I'm a beginner and step by step instructions would be great.
 
Thanks in advance.
Posted 12-May-13 14:13pm
Edited 12-May-13 14:40pm
v3
Comments
_Maxxx_ at 12-May-13 21:25pm
   
When you say you are keeping the UCs loosely coupled - just how loose? i.e. do you have a reference to the Children in your parent?
If you do have a reference then there's no real difference between there being a user control on the other tabs and, say, a text box. When your parent saves, it needs to access the data from the child controls and save it too.
It's difficult to do step by step instructions without seeing what you've already done - because there are simply so many ways you may have implemented it already.
_Maxxx_ at 13-May-13 0:56am
   
No worries. Ive looked at your code and am finding it difficult to follow what you're trying to do.
I THINK what you are asking for is simple - but I may be missing the point.
In UC2...UC99 have a public property that returns the data to be added/updated to the database.
Then in your update process you can call your WriteToDatabaseMCD() method passing that property as the parameter.
If necessary, the Getter of the property in UC2...UC99 will need to build your ManCageData object before returning it.
Another public property may be required for the IsUpdate parameter.
SDavisworth at 13-May-13 2:27am
   
Just been playing with tabs and events and have noticed that any values from uC2 return null error if focus on it is lost, hence your comment about rebuilding ManCageData before returning it (I guess). You have interpreted my requirements to the T. Is it possible to maintain my "WriteToDatabase" methods within each uC and have it processing its own added/updated methods... beginning to lose myself in all this code. PS. In your reply were you actually suggesting a way to do it or was it your synopsis of what you thought I was trying to accomplish? :-)
_Maxxx_ at 13-May-13 2:57am
   
If something like returning null is happening when you lose focus then something is wrong. My comment about rebuilding the data was not related to that - it's just that you need something to tell the child control "give me your data now" - and assuming on the child control there is a bunch of controls which the user has changed (textboxes or whatever) then the child control would have to build the object to return to the parent for it to save.
Of course you can have a write to database method in each UC - then all you need to do is to call that method on each of your child controls when the user clicks the save button on the parent control.
Personally I tend to keep any user controls small and low on functionality - so just capture the data and return it via a property. The Controller (in your case the parent control) then does the dirty work like saving it to the database.
There are arguments for doing it either way, though.
 
I was suggesting what you could do - it wasn't a synopsys.
 
SDavisworth at 13-May-13 0:48am
   
Got a ref to my Certificate repository as follows…
using PTWS.MainPanelControls.FormControls.Certificates;
 
To load uC2 into tab item dependant on checkbox….
 
else if (e.Command == cmdManCageRequiredYN)
{
switch (checkBoxManCageRequired.IsChecked)
{
case true:
stackPanelManCageDetail.Children.Clear();
_currentManCageDetail = new ManCageDetailUControl();
stackPanelManCageDetail.Children.Add(_currentManCageDetail);
tabManCageRequired.Visibility = Visibility.Visible;
IInputElement ieIn = Keyboard.Focus(tabManCageRequired);
break;
case false:
stackPanelManCageDetail.Children.Clear();
tabManCageRequired.Visibility = Visibility.Collapsed;
IInputElement ieOut = Keyboard.Focus(tabWorkPermit);
break;
}
 
Then update button is hooked into CommandBindings

 

Validation as follows:
private void Validation_Error(object sender, ValidationErrorEventArgs e)
{
if (e.Action == ValidationErrorEventAction.Added)
_noOfErrorsOnScreen++;
else
_noOfErrorsOnScreen--;
}
 
_Executed Routed Event as follows:
PermitData updateDraftPermit = new PermitData
{
… Blah blah
};
 
WriteToDataBase(updateDraftPermit, isUpdate);
 
Write to database – WriteToDataBase
private void WriteToDataBase(PermitData tableData, bool isUpdate)
{
#region Add New
if (!isUpdate)
{
db = new PTWDatabase();
Dictionary data = new Dictionary();
data.Add("Status", tableData.PermitStatus);
data.Add… blah blah
try
{
db.Insert("Permits", data);
}
 
catch (Exception fail)
{
String error = "The following error has occurred:\n\n";
error += fail.Message.ToString() + "\n\n";
MessageBox.Show(error);
}
}
#endregion
 
#region Update
else
{
db = new PTWDatabase();
Dictionary data = new Dictionary();
data.Add("Status", tableData.PermitStatus);
data.Add… blah blah
 
try
{
db.Update("Permits", data, String.Format("Permits.PermitID = {0}", tableData.PermitID));
}
 
catch (Exception fail)
{
String error = "xxThe following error has occurred:\n\n";
error += fail.Message.ToString() + "\n\n";
MessageBox.Show(error);
}
}
#endregion
}
 
private void WriteToDataBaseMCD(ManCageData tableData, bool isUpdate)
{
#region Add New
if (!isUpdate)
{
db = new PTWDatabase();
Dictionary data = new Dictionary();
data.Add("ManCageNumber", tableData.ManCageNumber);
 
try
{
db.Insert("CertificateManCage", data);
}
 
catch (Exception fail)
{
String error = "The following error has occurred:\n\n";
error += fail.Message.ToString() + "\n\n";
MessageBox.Show(error);
}
SDavisworth at 13-May-13 0:40am
   
I've created my uC2 in same fashion and now need the update mechanism on uC1 to trigger same update process on uC2… do you need full code?
SDavisworth at 13-May-13 0:48am
   
Thanks for prompt reply _Maxxx_
 
Got a ref to my Certificate repository as follows…
using PTWS.MainPanelControls.FormControls.Certificates;
 
To load uC2 into tab item dependant on checkbox….
 
else if (e.Command == cmdManCageRequiredYN)
{
switch (checkBoxManCageRequired.IsChecked)
{
case true:
stackPanelManCageDetail.Children.Clear();
_currentManCageDetail = new ManCageDetailUControl();
stackPanelManCageDetail.Children.Add(_currentManCageDetail);
tabManCageRequired.Visibility = Visibility.Visible;
IInputElement ieIn = Keyboard.Focus(tabManCageRequired);
break;
case false:
stackPanelManCageDetail.Children.Clear();
tabManCageRequired.Visibility = Visibility.Collapsed;
IInputElement ieOut = Keyboard.Focus(tabWorkPermit);
break;
}
 
Then update button is hooked into CommandBindings

 

Validation as follows:
private void Validation_Error(object sender, ValidationErrorEventArgs e)
{
if (e.Action == ValidationErrorEventAction.Added)
_noOfErrorsOnScreen++;
else
_noOfErrorsOnScreen--;
}
 
_Executed Routed Event as follows:
PermitData updateDraftPermit = new PermitData
{
… Blah blah
};
 
WriteToDataBase(updateDraftPermit, isUpdate);
 
Write to database – WriteToDataBase
private void WriteToDataBase(PermitData tableData, bool isUpdate)
{
#region Add New
if (!isUpdate)
{
db = new PTWDatabase();
Dictionary data = new Dictionary();
data.Add("Status", tableData.PermitStatus);
data.Add… blah blah
try
{
db.Insert("Permits", data);
}
 
catch (Exception fail)
{
String error = "The following error has occurred:\n\n";
error += fail.Message.ToString() + "\n\n";
MessageBox.Show(error);
}
}
#endregion
 
#region Update
else
{
db = new PTWDatabase();
Dictionary data = new Dictionary();
data.Add("Status", tableData.PermitStatus);
data.Add… blah blah
 
try
{
db.Update("Permits", data, String.Format("Permits.PermitID = {0}", tableData.PermitID));
}
 
catch (Exception fail)
{
String error = "xxThe following error has occurred:\n\n";
error += fail.Message.ToString() + "\n\n";
MessageBox.Show(error);
}
}
#endregion
}
 
private void WriteToDataBaseMCD(ManCageData tableData, bool isUpdate)
{
#region Add New
if (!isUpdate)
{
db = new PTWDatabase();
Dictionary data = new Dictionary();
data.Add("ManCageNumber", tableData.ManCageNumber);
 
try
{
db.Insert("CertificateManCage", data);
}
 
catch (Exception fail)
{
String error = "The following error has occurred:\n\n";
error += fail.Message.ToString() + "\n\n";
MessageBox.Show(er
_Maxxx_ at 13-May-13 0:57am
   
What the heck software is this anyway?! ManCage?! :)
SDavisworth at 13-May-13 2:15am
   
Lol... its a permit to work system that require safety cages to house workmen
SDavisworth at 13-May-13 21:36pm
   
Hi again _Maxxx_
In going down the path of keeping the UC as lean as possible, I've managed to set datacontext to uC2 from uC1 and is working well. I need to disable uC2 if checkbox on uC1 is unchecked... How would I do this? as well as how would I trigger uC2's validation from uC1. I've managed to disable uC1's "Update" button from uC2's Validation_error method, but it all falls apart when I try and "disable" uC2 via checkbox… this is when I get the “NullReferenceException” (I’m using stackPanelManCageDetail.Children.Clear(); to disable uC2 which me thinks is incorrect)
 
Sorry to be a pain by knocking on your door again…

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Have you looked into member attributes? One of the things you can do in the dot net framework, is create a custom attribute and then tag class members with that attribute. In other words, you can tag the properties you want to save within a specific control. Once you've tagged the parts you want to save, the parent control can loop through the collection of child controls and use reflection to get the properties that are tagged with your custom attribute. I once wrote a *.ini file reader writer that would serialize and deserialize various controls this way.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Richard MacCutchan 145
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 14 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100