【发布时间】:2018-03-01 07:40:58
【问题描述】:
我将我的ListBox 的ItemsSource 绑定为:
<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>
我的 ListBoX Xaml:
<ListBox x:Name="coloListBox" Style="{x:Null}"
ItemsSource="{Binding Source={StaticResource colorPropertiesOdp},
Converter={StaticResource ColSortConverter}}"
SelectedItem="{Binding SelectedColor,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent},Converter={StaticResource ColorToStringConverter}}">
<ListBox.ItemTemplate>
<DataTemplate>
<Rectangle Fill="{Binding Name}" Width="15" Height="15" Stroke="#FF211E1E" OpacityMask="Black" StrokeThickness="1"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="250" Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
转换器
public class ColorSortConverter : IValueConverter
{
public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
{
PropertyInfo[] colorsProperties = null;
var result = value as PropertyInfo[] ;
if (result != null)
{
colorsProperties = result;
colorsProperties.OrderBy(i => i.Name).ToArray();
return colorsProperties;
}
return colorsProperties;
}
}
如何对转换器中的数组进行排序,以得到按 HEX 值或亮度排序的颜色?
【问题讨论】:
-
简单地绑定到一个排序的集合不是更容易吗?