绑定数据源与一些枚举值

绑定数据源与一些枚举值

本文介绍了绑定数据源与一些枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ p

我的枚举:

  public enum过滤器:byte 
{
Filter1 = 1,
Filter2 = 2,
Filter3 = 4,
Filter4 = 8,
Filter5 = 16
}

所选值:

  public Filters SelectedFilters = Filters.Filter1 | Filters.Filter4; 

如何将 SelectedFilters 变量绑定为数据源?

解决方案

使用:

  public enum过滤器:byte 
{
Filter1 = 1,
Filter2 = 2,
Filter3 = 4,
Filter4 = 8,
Filter5 = 16
}

并选择一些值:

  public Filters SelectedFilters = Filters.Filter1 | Filters.Filter4; 

我创建了一个可以解决我自己的问题的方法:

  public static List< T> EnumToList< T>(Enum someEnum)
{
列表< T> list = new List< T>();

foreach(在Enum.GetValues(someEnum.GetType())中的var)
{
if((Convert.ToByte(it)& Convert.ToByte(someEnum) )!= 0)
{
list.Add((T)it);
}
}

返回列表;
}


myRepeater.DataSource = EnumToList< Filters>(SelectedFilters);


How can I bind a DataSource with some selected enum values?

My Enum:

public enum Filters : byte
{
    Filter1 = 1,
    Filter2 = 2,
    Filter3 = 4,
    Filter4 = 8,
    Filter5 = 16
}

Selected values:

public Filters SelectedFilters = Filters.Filter1 | Filters.Filter4;

How can I bind the SelectedFilters variable as a datasource?

解决方案

Using:

public enum Filters : byte
{
    Filter1 = 1,
    Filter2 = 2,
    Filter3 = 4,
    Filter4 = 8,
    Filter5 = 16
}

And selecting some values:

public Filters SelectedFilters = Filters.Filter1 | Filters.Filter4;

I created a method that can solve my own question:

public static List<T> EnumToList<T>(Enum someEnum)
{
    List<T> list = new List<T>();

    foreach (var it in Enum.GetValues(someEnum.GetType()))
    {
        if ((Convert.ToByte(it) & Convert.ToByte(someEnum)) != 0)
        {
            list.Add((T)it);
        }
    }

    return list;
}


myRepeater.DataSource = EnumToList<Filters>(SelectedFilters);

这篇关于绑定数据源与一些枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 05:50