简体   繁体   中英

Add an Id to ListBox item

I need to add an ID (x:Uid="identifier") to the last and first listbox item. My Listbox items are populated via data-source like this:

<ListBox x:Name="listbox1" Grid.Row="1" BorderThickness="0" 
     ItemsSource="{SomeBinding}">

Could it be done in xaml?

I'm not sure how you want to implement it, but here's some kinda of solution.

I figured you just need IDs of first and last items in your ListBox. Not sure you can do it in xaml without much work and headache. You already have it binded to some object. Let's say:

XAML:

<ListBox x:Name="listbox1" ItemsSource="{Binding Items}"/>

Code behind:

private ObservableCollection<Item> _Items;
public ObservableCollection<Item> Items
{
    get { return _Items; }
    set { _Items = value; }
}

So you could just get 'em like this:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var firstItem = Items[0];
    var lastItem = Items[Items.Count - 1];
}

Inherit from ListBox your custom class. Override protected method.. not quite remember which was the name... something about .GetContainerFor(...) . From there you might return new ListBoxItem() and set it up as you prefer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM