0

I want to access all the children of ItemControl. Actually, I want to access the button and change its background color.

<ItemsControl x:Name="ItemsControlReminder">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button x:Name="ButtonItemControlParent">
                <Grid Height="75">
                   .....
                   .....
                   .....
                </Grid>
            </Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

My constructor:

public RemindersPage() {
    InitializeComponent();

    List<Reminder> reminders = new List<Reminder> {........};
    ListViewReminder.ItemsSource = reminders;

    foreach ( var item in ListViewReminder.Items ) {
        UIElement uiElement = (UIElement) ListViewReminder.ItemContainerGenerator.ContainerFromItem(item);
    }
}

But it says uielement is null. Also I can't access the button with ui element. How do I access each button and change the background color?

Qwe Qwe
  • 399
  • 4
  • 13
  • 1
    This looks odd. Why don't you just bind the Button's Background property to a property of the Reminder item class? Or set it by a DataTrigger on a Reminder property. – Clemens Jan 07 '20 at 09:18
  • You can the FindAncestor Method to get the Button – AJAY KUMAR Jan 07 '20 at 10:13

0 Answers0