开发者

Bind ListBoxItems using Code

I try to bind XAML ListBoxItem using Code but doesn't seem work

In my XAML:

<Window.Resources>
    <local:FooList x:Key="FooListItem" />
</Window.Resources>

< ListBox x:Name="ListBox开发者_JS百科1" ItemsSource="{Binding Source={StaticResource FooListItem}}" />

In my code:

public class FooList
{
    add some items; // I tried variation of that but didn't get it to work
}

Any tips?


You don't create ListBoxItem from code.

You just provide a ListBox with collection of your CLR class objects, provide an ItemTemplate and it implicitly wraps an ItemTemplate inside a ListBoxItem.

Example:

public class FooList
{
    public ObservableCollection<String> Items { get; set; }

    public FooList()
    {
        Items = new ObservableCollection<String>();
    }
}

XAML:

<ListBox x:Name="ListBox1" ItemsSource="{Binding Path=Items, Source={StaticResource FooListItem}}" />


Programmatically binding List to ListBox

Listbox with custom drawn data bound objects

Data Binding in WPF

Windows Presentation Foundation Data Binding: Part 1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜