What is the best way to not tie DependencyProperties conflicts to actual properties?
I find that when I create dependency properties, most of them conflict with the property names in the UserControl like Background, Width, etc., so my strategy is to prefix all my custom properties with "The" , so I have, eg
- TheBackground
- TheWidth
and etc.
I tried to use the "new" keyword that gets rid of the warning, but this leads to runtime conflicts.
Can anyone find better naming strategies for DependencyProperties in custom controls?
public partial class SmartForm : UserControl
{
public SmartForm()
{
InitializeComponent();
DataContext = this;
TheBackground = "#FFD700";
}
#region DependencyProperty: TheBackground
public string TheBackground
{
get
{
return (string)GetValue(TheBackgroundProperty);
}
set
{
SetValue(TheBackgroundProperty, value);
}
}
public static readonly DependencyProperty TheBackgroundProperty =
DependencyProperty.Register("TheBackground", typeof(string), typeof(SmartForm),
new FrameworkPropertyMetadata());
#endregion
}
a source to share
If your UserControl has a background property, why should you add another one?
What is this new background for? "The"? No? Then what background does he control?
Complete this sentence: "This is the background color for the XXXX control."
The property name should now be XXXXBackground.
a source to share
Do you want you to redefine?
In my case, I did not set Width as DepedencyProperty
in my user control i have:
public double Width
{
get
{
if (_backgroundImage != null)
return _backgroundImage.Width;
else
return double.NaN;
}
set
{
if (_backgroundImage != null)
_backgroundImage.Width = value;
}
}
The compiler shows me a warning, but everything works.
a source to share