Custom Properties in XAML System.Windows.Window

I have a very simple WPF custom control that looks like this:

namespace WpfControlLibrary1
{
  public partial class UserControl1 : UserControl
  {
    public UserControl1()
    {
      InitializeComponent();
      Composite = new Composite();
      Composite.Color = Colors.Red;
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
      Draw(drawingContext, new Rect(RenderSize));
    }

    public void Draw(DrawingContext g, Rect rect)
    {
      Composite.Draw(g, rect);
    }

    public Composite Composite
    {
      get;
      set;
    }
  }

  public class Composite
  {
    public void Draw(DrawingContext g, Rect rect)
    {
      g.DrawRectangle(new SolidColorBrush(Color), new Pen(Brushes.Black, 1.0), rect);
    }

    public Color Color
    {
      get;
      set;
    }
  }
}

      

However, when I try to do this in the XAML of the window that the UserControl is sitting in:

<Window x:Class="WpfApplication1.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:test="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"
    Title="Window2" Height="500" Width="700">

  <test:UserControl1 Name="uControl1" Composite.Color="Blue">
  </test:UserControl1>
</Window>

      

I am getting the following errors:

Error   1   The attachable property 'Color' was not found in type 'Composite'.
Error   2   The property 'Composite.Color' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.

      

There should be an easy way to get the above to work, but I'm afraid I haven't been able to find any relevant information on this. Can someone please give me a pointer or two?

Many thanks!

0


source to share


1 answer


The syntax is Type.Property

used to set attached properties . Try this instead:



<test:UserControl1 Name="whatever">
    <test:UserControl1.Composite>
        <test:Composite Color="Blue"/>
    </test:UserControl1.Composite>
</test:UserControl1>

      

+4


source







All Articles