为所有列表框创建样式

时间:2018-02-20 17:33:38

标签: c# wpf

我正在开发一款新软件 此应用程序中有几个列表框,列表框只显示一张照片。我想为所有人定义一般风格

这是我的listbox代码

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

但不行 希望你帮忙 全部

1 个答案:

答案 0 :(得分:2)

在您的模板中,您仍然引用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

中找到更多信息