簡體   English   中英

是否可以將ItemsControl中的項目作為綁定的目標?

[英]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));

}

這個問題是對此處提出的問題的更簡潔的陳述:

我如何編寫此ItemsControl,以便WPF將使用綁定為輸出網格生成列

參見上面的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM