你想要一個MultiSelectList或ListPicker嗎?你想讓用戶能夠選擇多種顏色嗎?這是一個使用ListPicker的示例。要獲得顏色,您必須自己創建顏色。下面是一個示例使用口音的顏色
爲ListPicker的XAML:
<toolkit:ListPicker x:Name="ColorPicker" ExpansionMode="FullScreenOnly"
FullModeHeader="COLOR"
Visibility="Collapsed"
ItemsSource="{Binding Brushes}"
SelectedItem="{Binding SelectedBrush, Mode=TwoWay}">
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Margin="0 20" Orientation="Horizontal">
<Rectangle Width="42" Height="42" Fill="{Binding Brush}"
Stroke="{StaticResource PhoneForegroundBrush}"
HorizontalAlignment="Left"/>
<TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextExtraLargeStyle}"
HorizontalAlignment="Center" Margin="5,0"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
而產生的顏色的集合代碼:
private IList<ColorItem> CreateBrushes()
{
var brushes = new List<ColorItem>
{
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,27,161,226)), Name = "blue" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,160,80,0)), Name = "brown" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255, 51,153,51)), Name = "green" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,162,193,57)), Name = "lime" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,216,0,115)), Name = "magenta" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,240,150,9)), Name = "mango" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,230,113,184)), Name = "pink" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,162,0,255)), Name = "purple" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,229,20,0)), Name = "red" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,0,171,169)), Name = "teal" },
};
return brushes;
}
public class ColorItem
{
public SolidColorBrush Brush { get; set; }
public string Name { get; set; }
}
你也可以使用反射來獲取集合的顏色。
Type t = typeof(Colors);
var properties = t.GetProperties();
List<ColorItem> items = new List<ColorItem>();
for (int i = 0; i < properties.Length; i++)
{
var property = properties[i];
items.Add(new ColorItem
{
Name = property.Name,
Color = new SolidColorBrush((Color)property.GetValue(null, null))
});
}
感謝您的回覆。實際上,我喜歡用戶使用Multiselectlist選擇儘可能多的顏色。我已經看到了將重點顏色綁定到列表的示例,但是由於填充選項有多種內置顏色(例如白色煙霧,米色,DarkMagenta等),所以我想知道是否有更簡單的方法將這些顏色調用爲一個綁定到Multiselectlist的列表?我不知道如何獲得這些顏色,除了明確地設置前景,填充或其他任何它在xaml中,然後從填充的顏色選項列表中選擇結果。 – Matthew
增加了另一個解決方案,希望它有幫助 –