簡體   English   中英

WPF轉換器枚舉到數組

[英]WPF Converter Enum to Array

我正在開發WPF項目,我需要使用枚舉中存在的值填充組合框。

我想通過實現這種雙重轉換來做到這一點(請看下面的代碼)。 查看互聯網示例,我已經能夠實現一個方向,但是我不能解決相反的方向。

我對@BenRobinson的回答明確了我的確切目的: ComboBox綁定到枚舉,並綁定到一個包含SelectedItem的屬性,稱為OpenedResultFilter。 我希望在更改此屬性值時,組合框更新所選的項目。 對不起,造成混亂。.我對WPF不太熟練。

你能幫助我嗎?

<ComboBox 
MinWidth="80" 
ItemsSource="{Binding Converter={StaticResource EnumTypeToEnumArrayConverter}, ConverterParameter='Goldbet.GoldbetBackOffice.WPF.Model.ResultValidation.StagingOpenResultValidationFilter, Goldbet.GoldbetBackOffice.WPF.Model', Mode=OneWay}" 
SelectedItem="{Binding OpenedResultFilter, Mode=TwoWay}" 
HorizontalAlignment="Left"
Margin="1,2,1,2" 
DockPanel.Dock="Left"/>


<UserControl.Resources>
        <ResourceDictionary>

            <Converters:BoolNullableToBoolConverter x:Key="BoolNullableToBoolConverter" />
            <Converters:EnumTypeToEnumArrayConverter x:Key="EnumTypeToEnumArrayConverter"/>



public class StagingResultsBatchImportViewModel : WorkspaceViewModel, IHierarchyViewModel
    {
private StagingOpenResultValidationFilter _openedResultFilter = StagingOpenResultValidationFilter.Tutti;
        public StagingOpenResultValidationFilter OpenedResultFilter
        {
            get { return _openedResultFilter; }
            set
            {
                if (_openedResultFilter != value)
                {
                    _openedResultFilter = value;
                    OnPropertyChanged("OpenedResultFilter");
                }
            }
        }
}


public class EnumTypeToEnumArrayConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Type enumType = Type.GetType((string)parameter);
        return Enum.GetValues(enumType).Cast<int>().Select(p => Enum.ToObject(enumType, p));
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

如果您不使用MVVM(我懷疑您可以這樣做),它將在XAML中名為enumCombo的ComboBox中顯示所有Enum值:

public enum myEnum
{
    One,
    Two,
    Four,
    Eight
}

public partial class MainWindow : Window
{
    private IEnumerable<string> _enumValues;
    public MainWindow()
    {
        InitializeComponent();

        _enumValues = ConvertEnumToStrings<myEnum>();

        enumCombo.ItemsSource = _enumValues;
    }

    private static IEnumerable<string> ConvertEnumToStrings<T>()
    {
        var enumValues = Enum.GetValues(typeof(T))
            .Cast<T>()
            .Select(x => x.ToString())
            .OrderBy(x => x)
            .ToArray();

        return enumValues;
    }
}

如果您正在執行MVVM,則此枚舉轉換將在ViewModel中完成,並以字符串集合(如上)或封裝了(枚舉的)值和枚舉值的友好名稱(即名稱-值對)的ViewModel形式公開。在用戶界面中具有用戶友好的功能。

抱歉,打開這個問題使我一團糟。

實際問題是完全不同的,因此,如果該問題對社區沒有任何價值,請隨時投票關閉或刪除它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM