[英]Can an item in an ItemsControl be the target of a binding?
在下面的代碼中,我希望將第二項綁定到我的viewmodel的屬性。 我該怎么做? 我不想在代碼中創建列表或ObservableCollection。
<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));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.