c# - 类型 'Style' 的值不能添加到类型 'UIElementCollection' 的集合或字典中

标签 c# wpf xaml

我有以下 XAML 代码。我想要我的 ListView 中的备用行,这些行将在运行时通过代码动态填充。 我正在使用以下 XAML 代码,但它给出了名为 Error8 的错误“Style”类型的值无法添加到“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>

最佳答案

它需要在某种类型的 ResourceDictionary 中,您可以添加到 ListViewWindow/Page 资源,甚至是外部资源文件

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

所以把它添加到你上面的例子中

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

关于c# - 类型 'Style' 的值不能添加到类型 'UIElementCollection' 的集合或字典中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24795517/

相关文章:

c# - httpclient c# couchdb 未经授权创建数据库

c# - 如何在c#中动态删除一个文件?

javascript - 突出显示选定的表格单元格 C# 菜单

c# - 如何为整个 USB 驱动器制作校验和?

c# - 在 WPF 中在运行时呈现图像

wpf - 列表框 "IsSelected"绑定(bind)仅部分工作

c# - 更新依赖属性/附加属性中的普通属性,

c# - WPF:绑定(bind)到 MainWindow 属性

c# - 如何更改 XAMARIN 中 Picker 占位符的颜色?

xaml - 在 F# 中的自定义控件上创建 Xamarin.Forms BindableProperty