Silverlight 3 DataForm - 如何从枚举中填充DataFormComboBoxField

时间:2009-06-09 16:49:21

标签: silverlight silverlight-3.0 dataform

XAML

 <df:DataForm x:Name="MobCrud"
        AutoEdit="True"
        AutoCommit="True"
        AutoGenerateFields="False"
        VerticalAlignment="Top"       
        CommandButtonsVisibility="All"
        Header="Mob Details" 
        CanUserAddItems="True"
        CanUserDeleteItems="True"
        CurrentItem="{StaticResource newMob}"
    >
<df:DataForm.Fields>
   <df:DataFormTextField  Binding="{Binding Name}" FieldLabelContent="Name" />
   <df:DataFormTextField Binding="{Binding Title}" FieldLabelContent="Title"/>
   <df:DataFormComboBoxField  x:Name="AuraList" Binding="{Binding Aura}"  FieldLabelContent="Aura"/>
</df:DataForm.Fields>

代码:

public enum Auras
{
    Holy,
    Fire,
    Frost,
}

public class MobDetail : IEditableObject
{
    public string Name { get; set; }
    public string Title { get; set; }
    public Auras Aura { get; set; }

    public override string ToString() { return Name; }

    public void BeginEdit(){}
    public void EndEdit(){}
    public void CancelEdit(){}
}

DataForm ItemsSource绑定到ObservableCollection()

我需要做什么来填充和初始化下拉列表?

1 个答案:

答案 0 :(得分:1)

答案是使用转换器:

<df:DataFormComboBoxField  
     x:Name="AuraList"
     Binding="{Binding Aura, Mode=TwoWay,
               Converter={StaticResource enumSelectedValueConverter}}"
     FieldLabelContent="Aura"/>

并在表单Loaded event

上设置ItemsSource
(MobCrud.Fields[2] as DataFormComboBoxField).ItemsSource =
                 Enums.GetStringArray(typeof(Auras));

请参阅此处了解完整报道:

Creating-Rich-Data-Forms-in-Silverlight-3-Customization