XAML:
<Popup Name="popColorPicker"
Width="315"
Height="296"
AllowsTransparency="False"
IsOpen="{Binding IsColorPickerOpen}"
Placement="Mouse"
PopupAnimation="Fade"
StaysOpen="True">
<Border BorderThickness="0"
CornerRadius="4">
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<ItemsControl Margin="10" ItemsSource="{Binding Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border x:Name="ColorBorder"
Width="24"
Height="24"
Margin="8"
Background="{Binding Brush}"
BorderThickness="0"
CornerRadius="2"
Cursor="Hand"
Tag="{Binding}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding Path=DataContext.ColorPickedCommand, ElementName=Main}" CommandParameter="{Binding Tag, ElementName=ColorBorder}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Border>
</Popup>
VM:
public List<BrushInfo> Items { get; private set; }
private void IniColors()
{
var brushes = typeof(Brushes).GetProperties(BindingFlags.Static | BindingFlags.Public);
var query =
from propertyInfo in brushes
let brush = propertyInfo.GetValue(null, null) as SolidColorBrush
//where brush != null
orderby brush.Color.G
select new BrushInfo(propertyInfo.Name, brush);
Items = query.ToList();
}
private async Task OnColorPicked(object arg)
{
//todo:
//...
IsColorPickerOpen = false;
}
public class BrushInfo
{
private readonly SolidColorBrush brush;
public BrushInfo(string name, SolidColorBrush brush)
{
this.brush = brush;
Name = name;
}
public Brush Brush => brush;
public string Name { get; }
public string Hex => brush.Color.ToString();
}