我需要将所有颜色从“颜色”类添加到组合框,但不设置“透明”。我知道它是如何制作的,但这是另外一个条件-我必须使用绑定来完成所有操作。
我有:
<Window.Resources>
<ObjectDataProvider ObjectInstance="{x:Type Colors}" MethodName="GetProperties" x:Key="colorPropertiesOdp" />
</Window.Resources>
<ComboBox ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" DisplayMemberPath="Name" SelectedValuePath="Name"/>
它提供所有颜色。但是我不知道如何删除透明。
感谢帮助!
我想不出一个纯粹的XAML解决方案来解决这个问题。即使是带有过滤器的CollectionViewSource,根据您的方法,也需要在代码背后的代码或视图模型中使用函数。因此,您可以在两端保存一些代码,只需在后端列表附加到组合框之前对其进行过滤。为了简单起见,下面的代码使用背后的窗口代码而不是视图模型。
在后端:
public static IEnumerable<String> ColorsWithoutTransparent
{
get
{
var colors = typeof (Colors);
return colors.GetProperties().Select(x => x.Name).Where(x => !x.Equals("Transparent"));
}
}
修改的XAML(注意添加的Window DataContext):
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox Margin="50" ItemsSource="{Binding ColorsWithoutTransparent}"/>
</Grid>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句