Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF组合框中的所有颜色,不带一个_C#_Wpf_Combobox - Fatal编程技术网

C# WPF组合框中的所有颜色,不带一个

C# WPF组合框中的所有颜色,不带一个,c#,wpf,combobox,C#,Wpf,Combobox,我需要把所有的颜色从类颜色组合框,但没有透明。我知道它是如何制作的,但这是另外一个条件——我必须使用绑定来完成所有工作 我有: <Window.Resources> <ObjectDataProvider ObjectInstance="{x:Type Colors}" MethodName="GetProperties" x:Key="colorPropertiesOdp" /> </Window.Resources> <ComboBox

我需要把所有的颜色从类颜色组合框,但没有透明。我知道它是如何制作的,但这是另外一个条件——我必须使用绑定来完成所有工作

我有:

<Window.Resources>
    <ObjectDataProvider  ObjectInstance="{x:Type Colors}" MethodName="GetProperties" x:Key="colorPropertiesOdp" />
</Window.Resources>

 <ComboBox ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" DisplayMemberPath="Name"  SelectedValuePath="Name"/>
它提供所有颜色。但我不知道如何删除透明

<Window.Resources>
    <ObjectDataProvider  ObjectInstance="{x:Type Colors}" MethodName="GetProperties" x:Key="colorPropertiesOdp" />
    <CollectionViewSource x:Key="FilterCollectionView" Filter="CollectionViewSource_Filter" Source="{StaticResource colorPropertiesOdp}" />
</Window.Resources>

<ComboBox ItemsSource="{Binding Source={StaticResource FilterCollectionView}}" DisplayMemberPath="Name"  SelectedValuePath="Name"/>

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
    {
        System.Reflection.PropertyInfo pi = (System.Reflection.PropertyInfo)e.Item;
        if (pi.Name == "Transparent")
        {
            e.Accepted = false;
        }
        else
        {
            e.Accepted = true;
        }
    }
}

谢谢你的帮助

您可以将其分配给CollectionViewSource并过滤透明的

<Window.Resources>
    <ObjectDataProvider  ObjectInstance="{x:Type Colors}" MethodName="GetProperties" x:Key="colorPropertiesOdp" />
    <CollectionViewSource x:Key="FilterCollectionView" Filter="CollectionViewSource_Filter" Source="{StaticResource colorPropertiesOdp}" />
</Window.Resources>

<ComboBox ItemsSource="{Binding Source={StaticResource FilterCollectionView}}" DisplayMemberPath="Name"  SelectedValuePath="Name"/>

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
    {
        System.Reflection.PropertyInfo pi = (System.Reflection.PropertyInfo)e.Item;
        if (pi.Name == "Transparent")
        {
            e.Accepted = false;
        }
        else
        {
            e.Accepted = true;
        }
    }
}

我想不出一个解决这个问题的纯XAML解决方案。即使是带有过滤器的CollectionViewSource也需要codebehind或viewmodel中的函数,具体取决于您的方法。因此,您可以在两端保存一些代码,并在列表附加到组合框之前在后端过滤列表。为了简单起见,下面的代码使用窗口的codebehind而不是viewmodel

在后端:

public static IEnumerable<String> ColorsWithoutTransparent
{
    get
    {
        var colors = typeof (Colors);
        return colors.GetProperties().Select(x => x.Name).Where(x => !x.Equals("Transparent"));
    }
}
修改的XAML注意添加的窗口数据上下文:

<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>