문제

매우 간단한 WPF UserControl이 있습니다.

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

그러나 USERCONTROL이 앉아있는 창의 XAML 에서이 작업을 수행하려고 할 때 다음과 같습니다.

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

다음 오류가 발생합니다.

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

위의 작업을 수행하는 간단한 방법이 있어야하지만 주제에 대한 관련 정보를 찾을 수 없었습니다. 누구든지 나에게 포인터를 줄 수 있습니까?

많은 감사합니다!

도움이 되었습니까?

해결책

구문 Type.Property 설정하는 데 사용됩니다 첨부 된 속성. 대신 시도해보십시오.

<test:UserControl1 Name="whatever">
    <test:UserControl1.Composite>
        <test:Composite Color="Blue"/>
    </test:UserControl1.Composite>
</test:UserControl1>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top