Domanda

Ho un UserControl WPF molto semplice che assomiglia a questo:

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;
    }
  }
}

Tuttavia, quando provo a farlo nell'XAML della finestra in cui si trova UserControl:

<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>

Ottengo i seguenti errori:

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'.

Deve esserci un modo semplice per far funzionare quanto sopra, ma temo di non essere stato in grado di trovare informazioni rilevanti sull'argomento. Qualcuno può darmi un puntatore o due?

Mille grazie!

È stato utile?

Soluzione

La sintassi Type.Property viene utilizzata per impostare proprietà associate . Prova invece questo:

<test:UserControl1 Name="whatever">
    <test:UserControl1.Composite>
        <test:Composite Color="Blue"/>
    </test:UserControl1.Composite>
</test:UserControl1>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top