2015-06-24 70 views
0

在下面的代碼中,我想要第二個項目綁定到我的viewmodel屬性。 我該如何做到這一點?我不想在代碼中創建一個列表或ObservableCollection。ItemsControl中的項目是否可以作爲綁定的目標?

<ItemsControl> 
    <ItemsControl.Items> 
     <local:InfoTableItem Data="Hi there!"/> 
     <local:InfoTableItem Data="{Binding MyProperty}"/> 
    </ItemsControl.Items> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Text="{Binding Data}"/> 
      </StackPanel> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 




public class InfoTableItem : DependencyObject 
{ 

    public string Data 
    { 
     get { return (string)GetValue(DataProperty); } 
     set { SetValue(DataProperty, value); } 
    } 

    public static readonly DependencyProperty DataProperty = 
     DependencyProperty.Register("Data", typeof(string), typeof(InfoTableItem), new PropertyMetadata(String.Empty)); 

} 
+0

那麼你在運行時遇到錯誤消息還是編譯器錯誤? – nvoigt

+0

兩者都不顯示任何內容。 – Sam

+0

你有沒有看到你的輸出窗口,綁定錯誤出現在哪裏? – nvoigt

回答

相關問題