简体   繁体   English

在用户控件中公开控件

[英]Exposing controls within a usercontrol

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. 我们创建了一些Usercontrols,我们使用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, 问候,

FB ten Kate FB十凯特

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 显然可以使用几个属性执行此操作,在您添加DesignerSerializationVisibilityAttribute的Control属性上: http//msdn.microsoft.com/en-us/library/system.componentmodel.designerserializationvisibilityattribute.aspx ,您将其设置为内容: 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 :) 我同意Rob的意见,虽然拥有一个代表这个属性的房产是一个更好的做法,但这是什么,并回答我的问题,我到底怎么做:)

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. 例如,如果您将某个父usercontrol的子控件从datepicker更改为文本框,则只需更改已添加到父usercontrol的属性,您就不必更新10-20使用此用户控件的表单。

public class CustomerEditor
{
    public DateTime? Birthday
    {
        get
        {
            //probably tryparse but you get the idea
            return DateTime.Parse(birthdayPickerTextBox.Text); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM