创建一个显示所有预定义WPF颜色的ListBox

原文 Creating a ListBox that Shows All Predefined WPF Colors

在WPF中,您可以使用Colors类访问一系列预定义颜色,这些颜色定义为Colors类的静态属性。您只需使用颜色名称引用每种颜色。

作为参考,这里是一个小应用程序,显示ListBox中的所有颜色。(感谢casperOne,在stackoverflow文章中展示了如何创建一个封装Colors类中属性列表的对象)。

这是最终的结果。(单击图像可查看其全尺寸)。

用于生成此列表的XAML非常简单:

<Window
    x:Class="WpfApp5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    x:Name="Window"
    Title="All Colors"
    Width="800"
    Height="600"
    mc:Ignorable="d">

    <Window.Resources>
        <ObjectDataProvider
            x:Key="colorsTypeOdp"
            MethodName="GetType"
            ObjectType="{x:Type sys:Type}">
            <ObjectDataProvider.MethodParameters>
                <sys:String>
                    System.Windows.Media.Colors, PresentationCore,
                    Version=3.0.0.0, Culture=neutral,
                    PublicKeyToken=31bf3856ad364e35
                </sys:String>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

        <ObjectDataProvider
            x:Key="colorPropertiesOdp"
            MethodName="GetProperties"
            ObjectInstance="{StaticResource colorsTypeOdp}" />
    </Window.Resources>

    <ListBox
        ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}"
        ScrollViewer.HorizontalScrollBarVisibility="Disabled"
        ScrollViewer.VerticalScrollBarVisibility="Auto">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <Rectangle
                        Width="81"
                        Height="50"
                        Margin="4"
                        Fill="{Binding Path=Name}"
                        Stroke="Black"
                        StrokeThickness="1" />
                    <Label Content="{Binding Path=Name}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Window>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值