Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8695647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:05:22+00:00 2026-06-13T01:05:22+00:00

I want to set a DataTrigger for multiple DataTemplate but I am not sure

  • 0

I want to set a DataTrigger for multiple DataTemplate but I am not sure if there is way to do that?
I have view structured like below

<ItemsControl ItemsSource="{Binding Collection}"
              AlternationCount="2"
              ItemTemplateSelector="{StaticResource RowSelector}" >
<local:HeaderTemplateSelector x:Key="RowSelector"
            NormalTemplate="{StaticResource NormalTemplate}"
            ExpanderTemplate1="{StaticResource ExpanderTemplate1}"
            ExpanderTemplate2="{StaticResource ExpanderTemplate2}"
            ExpanderTemplate3="{StaticResource ExpanderTemplate3}"  />
<DataTemplate x:Key="NormalTemplate" ...>
    <stackpanel  x:Name="main"
<DataTemplate x:Key="ExpanderTemplate1" ...>
    <Grid  x:Name="main" ..>
<DataTemplate x:Key="ExpanderTemplate2" ...>
    <Expander  x:Name="main"
<DataTemplate x:Key="ExpanderTemplate3" ...>
    <Textblock  x:Name="main" ...>

I want this data trigger which provides alternative background for rows, the trigger defined below

<DataTemplate.Triggers>
    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
        <Setter Property="Background" Value="Blue" TargetName="main"/>
    </Trigger>
    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
        <Setter Property="Background" Value="black"  TargetName="main"/>
    </Trigger>
</DataTemplate.Triggers>

I can add this to the end of each DataTemplate and it works fine but it mean that I have to duplicate the some code 4 times, is there any way to avoid that? (I know I can use datagrid but it is overkill for this simple structure)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:05:24+00:00Added an answer on June 13, 2026 at 1:05 am

    You can try to set the trigger with the style:

    <Style TargetType="Panel" x:Key="AlternatelyPainted">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)}" Value="0">
                <Setter Property="Background" Value="Red"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)}" Value="1">
                <Setter Property="Background" Value="Blue"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    

    (code thankfully stolen from this answer) and use it in your templates:

    <DataTemplate x:Key="NormalTemplate" ...>
        <StackPanel Style="{StaticResource AlternatelyPainted}">
            ...
        </StackPanel>
    </DataTemplate>
    

    etc.

    Edit: if you want the style to be universal, not Panel-specific, you can try the following trick:

    <Style x:Key="AlternatelyPainted">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)}" Value="0">
                <Setter Property="Panel.Background" Value="Red"/>
                <Setter Property="Control.Background" Value="Red"/>
                <Setter Property="TextBlock.Background" Value="Red"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)}" Value="1">
                <Setter Property="Panel.Background" Value="Blue"/>
                <Setter Property="Control.Background" Value="Blue"/>
                <Setter Property="TextBlock.Background" Value="Blue"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    

    (removed the TargetType and added explicit types to the property names). Note that TextBlock doesn’t inherit its Background, so we need an additional line for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want set Listbox background to transparent but not working Is there any idea?
i have a multiple marker Google map, it works fine but i want set
I want to have one Style DataTrigger inside my window.resources that can be used
I want to create a datatemplate (in code, but thats not the point) which
i want set the visibility to itemized overlay in map view. if the zoom
i want set class for repeater when doPostBack for those dataID i wan but
My question is : I have an EditText and I want set a margin
i have a problem, i want set text of a UILabel or UItextView or
I want to set a repeating alarm monthly; but my app resets the alarm
I want to set the type of a list to be ints but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.