M
M
Maxim Shimko2017-08-28 20:55:57
WPF
Maxim Shimko, 2017-08-28 20:55:57

How to access an item in an ItemsControl by its index in WPF C#?

You need to access the button in the ItemsControl by index. For example, I have 100 entries and I want to get an instance of the 5th button so that I can change it as I want. Source xaml:

<ItemsControl x:Name="mainList">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Height="40" Margin="1 1 2 3">
                
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="40"></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition Width="50"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                
                <Button x:Name="Button" Tag="{Binding}" Click="Button_Click">
                    <Image Source="/Resources/button.png"></Image>
                </Button>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

I want to work with the Button button and the picture enclosed in it. It is important that you do not need an event of the Click type, where the object is passed, but to get it from the mainList itself by the type mainList[5].Button...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Silin, 2017-08-28
@byme

ItemsControl doesn't support selection, use ListBox there SelectedIndex
You can get by index in different ways.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question