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.
<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.