0

Inside an ItemsControl, I am trying to set a property of a custom UserControl conditionally using a DataTrigger, however, the DataContext in the DataTrigger is that of the UserControl, and not the current item in the list.

In the XAML below Status is a property of an item in ListOfItems. How can I bind to the Status property of the current list item?

Edit: In local:Icon I have set the DataContext, without setting it does work, but I want to know if it is also possible otherwise.

Wrong Data Context

<ItemsControl Margin="5 0 0 0" ItemsSource="{Binding ListOfItems}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Style="{StaticResource HorizontalStackPanel}">
                <local:Icon Width="18" Height="18">
                    <local:Icon.Style>
                        <Style TargetType="local:Icon">
                            <Style.Triggers>
                                <!-- DataContext is Icon, but I want current item from ListOfItems -->
                                <DataTrigger Binding="{Binding Status}" Value="Default">
                                    <Setter Property="Background" Value="Red" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </local:Icon.Style>
                </local:Icon>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

I know that RelativeSource can be used to access for example a parent DataContext, but I do not know how or if this can be used for my problem.

Joery
  • 759
  • 4
  • 13
  • 33
  • "*In local:Icon I have set the DataContext*" - that is a programming error, because it effectively breaks any DataContext-based Bindings of the control's properties. The control won't work as expected. – Clemens Aug 01 '21 at 16:52
  • @Clemens ha yes, shortly after answering the question I noticed, writing the question gave me some clarity :)! Thank you! – Joery Aug 01 '21 at 17:44

0 Answers0