C# 将属性绑定到自定义ItemTemplate
我有一个ListView和一个自定义ItemTemplate(MyUserControl1)C# 将属性绑定到自定义ItemTemplate,c#,wpf,listview,binding,itemtemplate,C#,Wpf,Listview,Binding,Itemtemplate,我有一个ListView和一个自定义ItemTemplate(MyUserControl1) MyUserControl1.cs仅包含一个属性(My段落): 公共段落我的段落 { 得到 { 返回我的段落; } 设置 { _我的段落=值; 段落=新段落(); 对于(int i=0;i
MyUserControl1.cs仅包含一个属性(My段落):
公共段落我的段落
{
得到
{
返回我的段落;
}
设置
{
_我的段落=值;
段落=新段落();
对于(int i=0;i<\u myparagration.Inlines.Count;i++)
{
添加(_myparation.Inlines[i]);
richTextBlock.Blocks.Add(段落);
}
}
}
我得到的错误是:
未能分配给属性“App4.MyUserControl1.MyParaguation”
我已经从Flex迁移,所以我做了一些错误的事情,但我不知道在哪里。为了绑定数据,目标属性必须是dependancProperty,所以为了使绑定工作正常,
myparagration
应该是UserControl中的dependancProperty,如下所示
public Paragraph MyParagraph
{
get { return (Paragraph)this.GetValue(MyParagraphProperty); }
set { this.SetValue(MyParagraphProperty, value); }
}
public static readonly DependencyProperty MyParagraphProperty = DependencyProperty.Register(
"MyParagraph", typeof(Paragraph), typeof(UserControl1),new PropertyMetadata(null));
public Paragraph MyParagraph
{
get
{
return _MyParagraph;
}
set
{
_MyParagraph = value;
Paragraph paragraph = new Paragraph();
for (int i = 0; i < _MyParagraph.Inlines.Count; i++)
{
paragraph.Inlines.Add(_MyParagraph.Inlines[i]);
richTextBlock.Blocks.Add(paragraph);
}
}
}
public Paragraph MyParagraph
{
get { return (Paragraph)this.GetValue(MyParagraphProperty); }
set { this.SetValue(MyParagraphProperty, value); }
}
public static readonly DependencyProperty MyParagraphProperty = DependencyProperty.Register(
"MyParagraph", typeof(Paragraph), typeof(UserControl1),new PropertyMetadata(null));