Ekta
Ekta

Reputation: 438

A value of type 'Style' cannot be added to a collection or dictionary of type 'UIElementCollection'

I have the following XAML code. I want alternate lines in my listview which will be populated dynamically though the code at run time. I'm using the following XAML code but it give error called Error8 A value of type 'Style' cannot be added to a collection or dictionary of type 'UIElementCollection'.

 <ListView Grid.Column="1" Grid.Row="10" BorderBrush="#FFA8CC7B" Height="133" HorizontalAlignment="Left" Margin="0,0,0,93" Name="lstViewAppsList" VerticalAlignment="Top" Width="596"
              ItemContainerStyle="{StaticResource alternatingStyle}" AlternationCount="2">
        <ListView.View>
            <GridView>
                <GridViewColumn  Header="Apps" Width="150" />
                <GridViewColumn  Header="Package" Width="350" />
            </GridView>
        </ListView.View>
    </ListView>

    <Style x:Key="alternatingStyle" TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                <Setter Property="BValue="LightSkyBlue"></Setter>
            </Trigger>
            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                <Setter Property="Background" Value="LightGray"></Setter>
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Orange"/>
            </Trigger>
        </Style.Triggers>
    </Style>

Upvotes: 1

Views: 10794

Answers (2)

sa_ddam213
sa_ddam213

Reputation: 43616

It needs to be in a ResourceDictionary of some sort, you can add to the ListView or the Window/Page resources or even an external resource file

<ListView.Resources>
   <Style x:Key="alternatingStyle" TargetType="{x:Type ListViewItem}">
       ......
   </Style>
<ListView.Resources>

So to add it to your example above

<ListView Grid.Column="1" Grid.Row="10" BorderBrush="#FFA8CC7B" Height="133" HorizontalAlignment="Left" Margin="0,0,0,93" Name="lstViewAppsList" VerticalAlignment="Top" Width="596" AlternationCount="2">
    <ListView.Resources>
       <Style TargetType="{x:Type ListViewItem}">
           ......
       </Style>
    <ListView.Resources>
     <ListView.View>
         <GridView>
             <GridViewColumn  Header="Apps" Width="150" />
             <GridViewColumn  Header="Package" Width="350" />
         </GridView>
     </ListView.View>
</ListView>

Upvotes: 6

user3222026
user3222026

Reputation: 81

You need to place this style in ResourceDictionary. If you are using , Place the style as follows,

<Window.Resources>
<Style x:Key="alternatingStyle" TargetType="{x:Type ListViewItem}">
</Style>
</Window.Resources>

Regards, Riyaj Ahamed I

Upvotes: 2

Related Questions