将ComboBox的ItemsSource绑定到WPF中的列表<dynamic>?

时间:2017-11-06 11:02:49

标签: c# wpf combobox

在我的应用程序中,我有这个ComboBox:

<ComboBox x:Name="cmbNationality" SelectedValuePath="NationalityId" 
  SelectedValue="{Binding Path=NationalityId, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" 
  Validation.Error="Validation_Error">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding NationalityAName}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

在某些模式下,我将其绑定到List<Nationality>,在另一种模式下,我想将其ItemsSource设置为List<dynamic>,其设置如下:

log.nationalities.Add(new { NationalityAName = 
((TbNationality)cmbNationality.SelectedItem).NationalityAName });

但是当我将ItemsSource设置为此动态列表时,ComboBox呈现为空:

cmbNationality.ItemsSource = log.nationalities;
cmbNationality.SelectedIndex = 0;

修改

这是调试winow: enter image description here

*请注意我不使用MVVM

0 个答案:

没有答案