代码之家  ›  专栏  ›  技术社区  ›  serialhobbyist

Silverlight数据表单如何自动生成从组合框到枚举的绑定?

  •  6
  • serialhobbyist  · 技术社区  · 15 年前

    我试图理解2009年11月工具包中实现的DataForm,但我不知道如何将组合框绑定到枚举。有人知道数据表单是如何自动执行的吗?

    背景

    首先,我创建了一个类和一个枚举,然后 this 并允许DataForm生成字段。DataForm为名称字符串字段生成了一个文本框,并且(我假设是)为Genres枚举字段生成了一个组合框。

    我理解如何定制数据表单的第一个目的是复制在自动生成中生成的内容。我设法完成了文本框(和datepicker,不包括在这段代码中),但我正在努力将组合框绑定到枚举。

    以下是类(简化):

    public class Movie
    {
        public string Name { get; set; }
        public Genres Genre { get; set; }
    }
    
    public enum Genres
    {
        Comedy,
        Fantasy,
        Drama,
        Thriller
    }
    

    然后在主页上我这样做:

    private ObservableCollection<Movie> movies = new ObservableCollection<Movie>();
    
    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        Movie movie = new Movie() { Name = "Fred", Genre = Genres.Thriller };
        movies.Add(movie);
        myDataForm.ItemsSource = movies;
    }
    

    在mainpage.xaml中,在网格中:

    <dataFormToolkit:DataForm x:Name="myDataForm" AutoEdit="False" AutoCommit="False"
                              Header="Foo Movie DB">
    </dataFormToolkit:DataForm>
    

    自动生成的内容。当尝试手动生成它时,我得到了:

    <dataFormToolkit:DataForm x:Name="myDataForm" AutoEdit="False" AutoCommit="False"
                              Header="Foo Movie DB">
        <StackPanel Orientation="Vertical">
            <dataFormToolkit:DataField>
                <TextBox Text="{Binding Name, Mode=TwoWay}"/>
            </dataFormToolkit:DataField>
            <dataFormToolkit:DataField>
                <ComboBox ItemsSource="{Binding Genres}"
                          SelectedItem="{Binding Genre, Mode=TwoWay}" />
            </dataFormToolkit:DataField>
        </StackPanel>
    </dataFormToolkit:DataForm>
    

    但是组合框不起作用。有很多文章都在讨论这个问题,但是对于一个自动生成器来说,他们提出的建议似乎太多了(例如,子类化组合框以提供selectedValue)。你知道这些工具是怎么为我们做的吗?

    2 回复  |  直到 15 年前
        1
  •  4
  •   Mark    15 年前

    这是DataForm执行的代码…

    ComboBox comboBox = new ComboBox();
    FieldInfo[] valueFieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.Static);
    List<string> valueList = new List<string>();
    foreach (FieldInfo valueFieldInfo in valueFieldInfos)
    {
                Enum value = valueFieldInfo.GetValue(null) as Enum;
                if (value != 0)
                {
                    valueList.Add(value.ToString());
                }
    }
    comboBox.ItemsSource = valueList;
    return comboBox;
    
        2
  •  5
  •   D.R. Payne    15 年前

    您可以使用ivalueConverter执行此操作:

    XAML:

                <ComboBox Width="100" Height="30" ItemsSource="{Binding GenreSource,Converter={StaticResource ec}}"
                      SelectedItem="{Binding SelectedGenre, Mode=TwoWay, Converter={StaticResource gc}}"></ComboBox>
    
    
        public class DemoViewModel : ViewModel
    {
        public DemoViewModel()
        {
        }
    
        public Type GenreSource
        {
            get
            {
                return typeof(Genres);
            }
        }
    
    
        private Genres _SelectedGenre;
    
        public Genres SelectedGenre
        {
            get { return _SelectedGenre; }
            set
            {
                _SelectedGenre = value;
                OnPropertyChanged("SelectedGrape");
            }
        }
    } 
    

    从枚举转换为组合框的列表:

    public class EnumListConverter : IValueConverter
    {
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var enumType = (Type)value;
            var names = new List<string>();
            foreach (var fieldInfo in enumType.GetFields(BindingFlags.Static | BindingFlags.Public))
            { 
                names.Add(fieldInfo.Name); 
            }
            return names;            
        }
    

    并从字符串转换回列表:

        public class GenreConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.ToString();
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (Views.Genres)Enum.Parse(typeof(Views.Genres), value.ToString(), false);
        }
    }
    

    您可以将完整的类型名传递给genrecoverter,使其成为任何枚举的泛型。

    推荐文章