I would like to build simple ColorComboBox, but I don't know, how do I get system colors(KnownColors) in Universal Windows Platform with c#. Type KnownColors is not accessible.
The Windows.UI.Colors class has properties for known colours from AliceBlue to YellowGreen. If you want a list of these colours you can use reflection to walk through the property names to build your own list to bind to.
For example:
A class to hold our color information
public class NamedColor
{
public string Name { get; set; }
public Color Color { get; set; }
}
And a property to bind to:
public ObservableCollection<NamedColor> Colors { get; set; }
Use reflection to build NamedColor list:
foreach (var color in typeof(Colors).GetRuntimeProperties())
{
Colors.Add(new NamedColor() { Name = color.Name, Color = (Color)color.GetValue(null) });
}
And some Xaml to bind to the color collection:
<ComboBox ItemsSource="{Binding Colors}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="0" Height="30" Width="30" Margin="2" VerticalAlignment="Center" Stroke="{ThemeResource SystemControlForegroundBaseHighBrush }" StrokeThickness="1">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Color}" />
</Rectangle.Fill>
</Rectangle>
<TextBlock Text="{Binding Name}" Grid.Column="1" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With