System.Windows.WindowのXAMLのカスタムプロパティ
-
03-07-2019 - |
質問
次のような非常にシンプルな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'.
上記を簡単に機能させる方法がなければなりませんが、このテーマに関する関連情報を見つけることができなかったのではないかと心配しています。誰か私にポインタを1つまたは2つください?
どうもありがとう!
解決
Type.Property を使用します。 >添付プロパティ。代わりにこれを試してください:
<test:UserControl1 Name="whatever">
<test:UserControl1.Composite>
<test:Composite Color="Blue"/>
</test:UserControl1.Composite>
</test:UserControl1>
所属していません StackOverflow