Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未应用列表框项目样式_C#_Wpf_Xaml - Fatal编程技术网

C# 未应用列表框项目样式

C# 未应用列表框项目样式,c#,wpf,xaml,C#,Wpf,Xaml,我正在尝试为我的ListBoxItem创建一个样式,但是没有应用该样式 谁能告诉我怎么了 代码: <ListBox> <ListBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel IsItemsHost="True" Orientation="Horizontal"/> <

我正在尝试为我的ListBoxItem创建一个样式,但是没有应用该样式

谁能告诉我怎么了

代码:

<ListBox>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel IsItemsHost="True" Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBoxItem Style="{StaticResource MyListStyle}" Content="Test1" Foreground="Gray"/>
    <ListBoxItem Style="{StaticResource MyListStyle}" Content="Test2" Foreground="Gray"/>
</ListBox>
<Style x:Key="MyListStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="Transparent"></Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Black" />
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="White"/>
            </Trigger>
        </Style.Triggers>
</Style>
XAML:

<ListBox>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel IsItemsHost="True" Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBoxItem Style="{StaticResource MyListStyle}" Content="Test1" Foreground="Gray"/>
    <ListBoxItem Style="{StaticResource MyListStyle}" Content="Test2" Foreground="Gray"/>
</ListBox>
<Style x:Key="MyListStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="Transparent"></Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Black" />
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="White"/>
            </Trigger>
        </Style.Triggers>
</Style>

风格:

<ListBox>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel IsItemsHost="True" Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBoxItem Style="{StaticResource MyListStyle}" Content="Test1" Foreground="Gray"/>
    <ListBoxItem Style="{StaticResource MyListStyle}" Content="Test2" Foreground="Gray"/>
</ListBox>
<Style x:Key="MyListStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="Transparent"></Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Black" />
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="White"/>
            </Trigger>
        </Style.Triggers>
</Style>


因为WPF属性的优先级从高到低排列

因此,您的ListBoxItem.前台是本地设置,高于其他设置

 <ListBoxItem Style="{StaticResource MyListStyle}" Content="Test2" Foreground="Gray"/>

您可以删除

<ListBoxItem Style="{StaticResource MyListStyle}" Content="Test2"/>

并以静态资源样式设置前景

<Style x:Key="MyListStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="Transparent"></Setter>
        <Setter Property="Foreground" Value="Gray"></Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Black" />
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="White"/>
            </Trigger>
        </Style.Triggers>
    </Style>

您可以在中找到优先级列表


还有一个问题,如何从盒子中去除蓝色效果?此属性的名称可以显示最终效果吗?在MyListStyle静态资源中尝试蓝色框仍然出现确定我看到了,要删除选择突出显示,请在此处回答