Whats wrong with this code. I am completely clueless.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Data;
namespace CustomControls
{
public class PercentFiller: StackPanel
{
public Rectangle _FillerRectangle = null;
public PercentFiller()
{
_FillerRectangle = new Rectangle();
_FillerRectangle.Height = this.Height;
_FillerRectangle.Width = 20;
_FillerRectangle.SetBinding(Rectangle.FillProperty, new Binding() {
Source = this,
Path = new PropertyPath("FillColorProperty"),
Mode = BindingMode.TwoWay
});
this.Background = new SolidColorBrush(Colors.LightGray);
this.Children.Add(_FillerRectangle);
this.UpdateLayout();
}
public static readonly DependencyProperty FillColorProperty = DependencyProperty.Register(
"FillColor",
typeof(Brush),
typeof(Compressor),
new PropertyMetadata(new SolidColorBrush(Color.FromArgb(255, 134, 134, 134))));
[Description("Gets or sets the fill color")]
public Brush FillColor
{
get { return (Brush) GetValue(FillColorProperty); }
set { SetValue (FillColorProperty, value); }
}
}
}
The Rectangle Control is not getting displayed, when i add this control to another project. Please somebody help me fixing this issue.
You got the binding wrong (your
_FillerRectanglehas noDataContext).Also, you can pass the dependency property itself to the
PropertyPath.Try changing your binding like this:
The
DataContext“tells” the binding where the dependency property you’re binding is located.Also, there’s an error in your
DependencyPropertydeclaration:The marked line should be the type of the property’s containing object, so in this case it should read
typeof(PercentFiller).UPDATE:
I forgot to add:
StackPaneldoesn’t have a size per-se, so:Is meaningless in this context. Either set a fixed size or change your control to inherit
Gridinstead ofStackPanel.