c# - 为所有列表框创建样式

标签 c# wpf

我正在开发一个新软件 该应用程序中有多个列表框,列表框仅显示一张照片。我想为它们定义一个通用样式

这是我的列表框代码

<ListBox Grid.Row="1" x:Name="HeroImageListbox" BorderThickness="0" Background="Transparent">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel IsItemsHost="True" Width="210"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Image Source="{Binding ImageSource}" Style="{StaticResource ImageHover}">
                            <Image.Effect>
                                <DropShadowEffect Direction="0" Color="Black" ShadowDepth="0" BlurRadius="35" Opacity="0.28"/>
                            </Image.Effect>
                        </Image>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="Padding" Value="0"/>
                        <Setter Property="Margin" Value="5 20 0 5"/>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>

并且我想对所有这些都使用我的样式,那么我如何将此样式放入资源目录并使用它

我试试:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:DotaDrafter.Styles">
<Style TargetType="ListBox">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" Width="210"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding ImageSource}" Style="{StaticResource ImageHover}">
                <Image.Effect>
                    <DropShadowEffect Direction="0" Color="Black" ShadowDepth="0" BlurRadius="35" Opacity="0.28"/>
                </Image.Effect>
            </Image>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Margin" Value="5 20 0 5"/>
        </Style>
    </ListBox.ItemContainerStyle>
</Style>

但不工作 希望你能帮忙 谢谢大家

最佳答案

在您的模板中,您仍然引用Listbox,请尝试使用 Setters:

<Style TargetType="ListBox">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Image Source="{Binding ImageSource}" Style="{StaticResource ImageHover}">
                    <Image.Effect>
                        <DropShadowEffect Direction="0" Color="Black" ShadowDepth="0" BlurRadius="35" Opacity="0.28"/>
                    </Image.Effect>
                </Image>
            </DataTemplate>
        </Setter.Value>
    </Setter>

    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ListBoxItem">
                <Setter Property="Padding" Value="0"/>
                <Setter Property="Margin" Value="5 20 0 5"/>
            </Style>
        </Setter.Value>
    </Setter>

    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True" Width="210"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

注意:确保您的资源中也有 ImageHover 的样式。

您可以找到有关列表框样式的更多信息 here

关于c# - 为所有列表框创建样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48891084/

相关文章:

c# - 纹理缺乏颜色

c# - 枚举更改事件

c# - WPF 某些图像在加载时旋转

c# - 带 Prism 区域适配器的 AvalonDock

c# - 如何使用 linq 返回一个数组中与另一个数组的整数属性不匹配的整数?

c# - 在 MVVM 应用程序中切换 ViewModel 时出现 BindingExpression 路径错误

c# - 控制线程数以平衡 .NET 中的 CPU 和 IO 等待

c# - 基本 WPF LiveCharts DateTime 示例不起作用

c# - 将复选框可见性绑定(bind)到转换后的 bool 值和另一个复选框可见性

C# | SharpDX.X输入法 |如何检测 Controller 上的按钮是否被按下一次并且没有被按下