Reputation: 2032
At the risk of beeing mocked and put on a spike for breaking some design rules i have the following problem.
We've created a few Usercontrols, we exposed the controls on these usercontrols by using Properties. We then change these problems in our designer to tweak the usercontrols further (for the specific forms) how ever when we save nothing is written by the designer and the changes are indeed not saved.
When we hardcode the changes into the designer file the changes get saved, and reaplied by if we then manually change a property through the designer and save all previous changes are discarted.
Are we breaking some pattern/design rule? Is this a bug, is there a better way to do this?
Greetings,
F.B. ten Kate
Upvotes: 3
Views: 3467
Reputation: 2032
Apperently it is possible to do this with a couple of attributes, on the Control property you add a DesignerSerializationVisibilityAttribute: http://msdn.microsoft.com/en-us/library/system.componentmodel.designerserializationvisibilityattribute.aspx, which you set as content: http://msdn.microsoft.com/en-us/library/system.componentmodel.designerserializationvisibility.aspx
I do agree with Rob though that having a property that delegates this is a better practise, how ever this is and answer to my question, of how the hell do i do this :)
Upvotes: 3
Reputation: 15621
If you create yourself a property on your parent user control that then simply delegates off to the child user control's property, then you should see the designer being able to catch up with you.
public class CustomerEditor
{
public DateTime? Birthday
{
get
{
return birthdayPicker.Date;
This has the added bonus that you're (slightly) protecting your client code (the designer generated stuff) from the specific implementation details of your parent user control. For example, if you change one of your parent usercontrol's child controls from a datepicker to a textbox, you'll only have to change the property you've added to the parent usercontrol, and you won't have to update the 10-20 forms that make use of this usercontrol.
public class CustomerEditor
{
public DateTime? Birthday
{
get
{
//probably tryparse but you get the idea
return DateTime.Parse(birthdayPickerTextBox.Text);
Upvotes: 2