0

我有一些 Metro/win8 应用程序的样式:

    <Style TargetType="ListViewItem">
        <Setter Property="Background" >
            <Setter.Value>
                <SolidColorBrush Color="#FF171717" Opacity="0.70"/>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderBrush" Value="#FFEAF32C" />
        <Setter Property="BorderThickness" Value="2, 0, 0, 0" />

        <Setter Property="Padding" Value="5" />
        <Setter Property="Opacity" Value="40" />
    </Style>

但是现在我正在 wpf (.net 4.5) 中制作桌面应用程序,并且无法在 xaml 中将这样的样式应用于 ListView 控件。我们如何在 xaml 中为桌面 ListView 控件定义我们自己的自定义样式?

4

1 回答 1

5

这是一个将样式放入 Windows 资源字典的示例。

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="ListViewItem">
            <Setter Property="Background" >
                <Setter.Value>
                    <SolidColorBrush Color="#FF171717" Opacity="0.70"/>
                </Setter.Value>
            </Setter>
            <Setter Property="BorderBrush" Value="#FFEAF32C" />
            <Setter Property="BorderThickness" Value="2, 0, 0, 0" />

            <Setter Property="Padding" Value="5" />
            <Setter Property="Opacity" Value="40" />
        </Style>
    </Window.Resources>
    <Grid>
        <ScrollViewer HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch">
            <ListView>
                <ListView.Items>
                    <Button>a</Button>
                    <Button>b</Button>
                    <Button>c</Button>
                    <Button>d</Button>
                    <Button>e</Button>
                </ListView.Items>
            </ListView>
        </ScrollViewer>
    </Grid>
</Window>

如果您希望将样式放在它自己的文件中,那么您可以像这样引用该文件(我的资源文件只是调用 Dictionary1.xaml)

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <ScrollViewer HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch">
            <ListView>
                <ListView.Items>
                    <Button>a</Button>
                    <Button>b</Button>
                    <Button>c</Button>
                    <Button>d</Button>
                    <Button>e</Button>
                </ListView.Items>
            </ListView>
        </ScrollViewer>
    </Grid>
</Window>
于 2013-02-28T12:26:06.543 回答