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 に答える