Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
<Window ...
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApplication1">
<Window.Resources>
<ObjectDataProvider x:Key="ColorsInstance" ObjectType="{x:Type sys:Type}" MethodName="GetType">
<ObjectDataProvider.MethodParameters>
<sys:String>System.Windows.Media.Colors, PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</sys:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<!-- 1 вариант вообще без дополнительных классов, но в шаблоне ComboBoxItem биндинг нужно осуществялть к Name -->
<ObjectDataProvider x:Key="AvailableColors1" ObjectInstance="{StaticResource ColorsInstance}" MethodName="GetProperties" />
<!-- 2 и 3 способы через класс-хелпер -->
<ObjectDataProvider x:Key="AvailableColors2" ObjectType="{x:Type local:ColorHelpers}" MethodName="GetAllColors"/>
<ObjectDataProvider x:Key="AvailableColors3" ObjectType="{x:Type local:ColorHelpers}" MethodName="GetAppColors"/>
<DataTemplate x:Key="ColorsComboBox1ItemTemplate" DataType="{x:Type ComboBoxItem}">
<StackPanel Height="18" Margin="0,0,0,2" Orientation="Horizontal">
<Border Width="30"
Background="{Binding Name}"
BorderBrush="DarkGray"
BorderThickness="1"
CornerRadius="2" />
<TextBlock Margin="8,0,0,0" Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
<Style x:Key="ColorsComboBox1Style" TargetType="{x:Type ComboBox}">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Width" Value="150" />
<Setter Property="ItemTemplate" Value="{StaticResource ColorsComboBox1ItemTemplate}" />
</Style>
<DataTemplate x:Key="ColorsComboBox2ItemTemplate" DataType="{x:Type ComboBoxItem}">
<StackPanel Height="18" Margin="0,0,0,2" Orientation="Horizontal">
<Border Width="30"
Background="{Binding}"
BorderBrush="DarkGray"
BorderThickness="1"
CornerRadius="2" />
<TextBlock Margin="8,0,0,0" Text="{Binding}" />
</StackPanel>
</DataTemplate>
<Style x:Key="ColorsComboBox2Style" TargetType="{x:Type ComboBox}">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Width" Value="150" />
<Setter Property="ItemTemplate" Value="{StaticResource ColorsComboBox2ItemTemplate}" />
</Style>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ComboBox ItemsSource="{Binding Source={StaticResource AvailableColors1}}" SelectedValuePath="Name" Style="{StaticResource ColorsComboBox1Style}" />
<ComboBox ItemsSource="{Binding Source={StaticResource AvailableColors2}}" Style="{StaticResource ColorsComboBox2Style}" Grid.Row="1" />
<ComboBox ItemsSource="{Binding Source={StaticResource AvailableColors3}}" Style="{StaticResource ColorsComboBox2Style}" Grid.Row="2" />
<ContentControl Content="{Binding Source={StaticResource AvailableColors3}}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="150" Grid.Row="3">
<ContentControl.ContentTemplate>
<DataTemplate>
<Grid>
<ComboBox x:Name="Items" ItemsSource="{Binding}"/>
<TextBlock x:Name="DefaultText" Text="Выберите цвет" IsHitTestVisible="False" Visibility="Hidden" Margin="4,3" />
</Grid>
<DataTemplate.Triggers>
<Trigger SourceName="Items" Property="SelectedItem" Value="{x:Null}">
<Setter TargetName="DefaultText" Property="Visibility" Value="Visible"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</Grid>
</Window>
public sealed class ColorHelpers
{
public enum AppColor : uint
{
Black = 0xFF000000,
Blue = 0xFF0000FF,
Brown = 0xFFA52A2A,
Crimson = 0xFFDC143C,
Cyan = 0xFF00FFFF
}
public static Color FromUInt32(uint argb)
{
var a = (byte)((argb & 0xff000000) >> 24);
var r = (byte)((argb & 0x00ff0000) >> 16);
var g = (byte)((argb & 0x0000ff00) >> 8);
var b = (byte)(argb & 0x000000ff);
return Color.FromArgb(a, r, g, b);
}
public static IEnumerable<string> GetAllColors()
{
return typeof(Colors).GetProperties(BindingFlags.Public | BindingFlags.Static).Select(p => p.Name);
}
public static IEnumerable<string> GetAppColors()
{
return typeof(AppColor).GetEnumNames(); //Enum.GetValues(typeof(AppColor)).GetNames()
}
}
<ComboBox">
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem>Выберите цвет</ComboBoxItem>
<CollectionContainer Collection="{Binding}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>

Построение графиков на WPF форме под .NET Framework 4