As the first preference, such controls can be internal
if they should be used visible in the container project. You can make them public based on requirements. Also being internal, they can be used in friend assemblies as well.
Anyway to reduce the difficulty of turning on/off of DesignTimeVisible
is what you are looking for, you can consider these options:
Option 1
As an option, you can decrease the difficulty of turning on/off the DesignTimeVisible
attribute. You can decorate all classes just once with DesignTimeVisible
but control its value from a central point.
To do so, create a class to hold setting:
public class MyGlobalSettings
{
public const bool DesignTimeVisible = false;
}
Then decorate controls this way:
[DesignTimeVisible(MyGlobalSettings.DesignTimeVisible)]
public partial class UserControl1 : UserControl
Then to turn on/off showing controls in toolbox, it's enough to set DesignTimeVisible
. This way, it's just a single point setting.
Option 2
An another option you can use a T4 Template to generate partial classes for your controls. In the file you can have a variable which will be used as value of DesignTimeVisible
attribute. Then in the T4 template, decorate all partial classes with DesignTimeVisible
having specified value. You can simply change the value in a single point.
Class names can be generated automatically using code, but in this example I used static class names:
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<# var designTimeVisibleValue = "false"; #>
using System.Windows.Controls;
using System.ComponentModel;
namespace WpfCustomControlLibrary1
{
[DesignTimeVisible(<#=designTimeVisibleValue#>)]
public partial class UserControl1 : UserControl{}
[DesignTimeVisible(<#=designTimeVisibleValue#>)]
public partial class UserControl2 : UserControl{}
}
Note
Also as mentioned in comments, you can use tools like Fody, PostSharp, dIHook,... to modify assembly at build time. Using these libraries just for such requirement is too much. Such tools can have lots of benefits but using them just for such requirement is too much and not a good idea.