2013-10-02 146 views
0

我有一個ListView和一個自定義ItemTemplate(MyUserControl1)。綁定屬性到自定義ItemTemplate

<ListView Name="listView" HorizontalAlignment="Left" Height="454" Margin="656,147,0,-461" VerticalAlignment="Top" Width="337"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0"> 
       <TextBlock Text="{Binding text}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap"/> 
       <local:MyUserControl1 MyParagraph="{Binding paragraph}"></local:MyUserControl1> 
       <TextBlock Text="test" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap"/> 
      </StackPanel> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

的MyUserControl1.cs僅包括一個屬性(MyParagraph):

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

我得到的錯誤是:

無法分配財產「App4.MyUserControl1.MyParagraph 」。

我已經從Flex遷移,所以我做錯了什麼,但我不知道在哪裏。

回答

0

爲了將數據綁定,目標屬性必須是DependancyProperty,所​​以爲了使這種結合工作,MyParagraph應該像下面

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)); 
+0

這工作完全在你的用戶控件一個dependancyproperty。非常感謝。 –

相關問題