C# Silverlight 5自定义控件工作不正常

C# Silverlight 5自定义控件工作不正常,c#,custom-controls,silverlight-5.0,C#,Custom Controls,Silverlight 5.0,这个代码有什么问题。我完全不懂 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.Animatio

这个代码有什么问题。我完全不懂

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); }
        } 
    }
}
当我将此控件添加到另一个项目时,不会显示矩形控件。请有人帮我解决这个问题。

您的绑定错误(您的
\u FillerRectangle
没有
DataContext
)。 此外,还可以将依赖项属性本身传递给
PropertyPath

尝试按以下方式更改绑定:

_FillerRectangle.DataContext = this; //#1: add this line
_FillerRectangle.SetBinding(Rectangle.FillProperty, new Binding()
{
    Path = new PropertyPath(FillColorProperty), // #2: no longer a string
    Mode = BindingMode.TwoWay
});
DataContext
“告诉”绑定您要绑定的依赖项属性所在的位置

另外,您的
dependencProperty
声明中有一个错误:

public static readonly DependencyProperty FillColorProperty = DependencyProperty.Register(
    "FillColor",
    typeof(Brush),
    typeof(Compressor), // <- ERROR ! should be typeof(PercentFiller)
    new PropertyMetadata(new SolidColorBrush(Color.FromArgb(255, 134, 134, 134))));
在这种情况下是没有意义的。设置固定大小或将控件更改为继承
网格
而不是
堆栈面板
绑定错误(您的
\u FillerRectangle
没有
数据上下文
)。 此外,还可以将依赖项属性本身传递给
PropertyPath

尝试按以下方式更改绑定:

_FillerRectangle.DataContext = this; //#1: add this line
_FillerRectangle.SetBinding(Rectangle.FillProperty, new Binding()
{
    Path = new PropertyPath(FillColorProperty), // #2: no longer a string
    Mode = BindingMode.TwoWay
});
DataContext
“告诉”绑定您要绑定的依赖项属性所在的位置

另外,您的
dependencProperty
声明中有一个错误:

public static readonly DependencyProperty FillColorProperty = DependencyProperty.Register(
    "FillColor",
    typeof(Brush),
    typeof(Compressor), // <- ERROR ! should be typeof(PercentFiller)
    new PropertyMetadata(new SolidColorBrush(Color.FromArgb(255, 134, 134, 134))));

在这种情况下是没有意义的。设置固定大小或将控件更改为继承
Grid
而不是
StackPanel

您可以通过
实现相同的结果,无需自定义控件。我正在通过一些示例学习自定义控件的概念。因此,我尝试了这一点,但没有成功:(你可以通过
获得相同的结果,不需要自定义控件。我只是试图通过一些示例学习自定义控件的概念。因此,我尝试了这一点,但没有成功:(我更改了它,但仍然有相同的行为。我能够为堆栈面板(父级)设置背景色),但不适用于矩形(子对象)问题列表进一步扩展。我做了一些愚蠢的事情。在我设置了高度后,它似乎起了作用。谢谢Alex!!不客气,记住如果它解决了问题,请将答案标记为已接受,以便让未来的旁观者知道。我已更改了答案,但仍然有相同的行为。我能够为堆栈面板(父级)设置背景色,但不是为了进一步扩展矩形(儿童)问题列表。我做了一些愚蠢的事情。在我设置了高度后,它似乎起了作用。谢谢Alex!!不客气,记住如果它解决了问题,请将答案标记为已接受,以便让未来的旁观者知道。