1. 程式人生 > >創建一個顯示所有預定義WPF顏色的ListBox

創建一個顯示所有預定義WPF顏色的ListBox

nta tps 技術分享 schema timestamp sps mpat box 有一個

原文 https://stuff.seans.com/2011/02/14/creating-a-listbox-that-shows-all-predefined-wpf-colors/

在WPF中,您可以使用Colors類訪問一系列預定義顏色,這些顏色定義為Colors類的靜態屬性只需使用顏色名稱引用每種顏色。

作為參考,這裏有一個小應用程序,顯示ListBox中的所有顏色(感謝casperOne,在stackoverflow文章中展示了如何創建一個封裝Colors中屬性列表的對象)。

這是最終的結果。(單擊圖像可查看其全尺寸)。

技術分享圖片

用於生成此列表的XAML非常簡單:

1
2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 三十 31 32 33 34 35 36 37 38 39 40 41 42 43 44
<Window 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" mc:Ignorable="d" xmlns:local="clr-namespace:WpfApplication1" xmlns:sys="clr-namespace:System;assembly=mscorlib" x:Class="WpfApplication1.MainWindow" x:Name="Window"
Title="All Colors" Width="640" Height="480" > <Window.Resources> <ObjectDataProvider MethodName="GetType" ObjectType="{x:Type sys:Type}" x:Key="colorsTypeOdp"> <ObjectDataProvider.MethodParameters> <sys:String>System.Windows.Media.Colors, PresentationCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</sys:String> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> <ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}" MethodName="GetProperties" x:Key="colorPropertiesOdp"> </ObjectDataProvider> </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 Fill="{Binding Path=Name}" Stroke="Black" Margin="4" StrokeThickness="1" Height="50" Width="81"/> <Label Content="{Binding Path=Name}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Window>

創建一個顯示所有預定義WPF顏色的ListBox