在通过反射创建表单时无法设置comboBox的ItemsSource吗?

时间:2017-10-23 13:01:32

标签: c# wpf data-binding combobox

我有这个代码在新选项卡中打开一个usercontrl,当双击网格中的一行时,我从中提取一些信息,并将一个对象传递给表单的构造函数:

private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
{
    var log = ((FrameworkElement)sender).DataContext as HistoryLog;
    if (log.JSONClassName != null && log.FormName != null)
    {
        try
        {
            using (ClsUserTransactions oUserTrans = new ClsUserTransactions())
            {
                string jsonLog = oUserTrans.GetJsonLog(log.Id);

                Type jsonType = Type.GetType(log.JSONClassName);
                var obj = Auditor.DeserializeObject(jsonLog, jsonType);
                var formType = Type.GetType("DiERP." + log.FormName);
                var oMainWindow = Window.GetWindow(this) as MainWindow;
                var frm = Activator.CreateInstance(formType, obj);
                oMainWindow.AddToTab(frm, log.ObjectName);
                e.Handled = true;
            }
        }
        catch (Exception ex)
        {

        }
    }
}

这是一个有问题的表单的构造函数:

public FrmArea(JClsTbArea log)
{
    InitializeComponent();
    spanMain.DataContext = log;
    cmbCountry.ItemsSource = log.CmbCountry; //List<TbCountry>
    cmbCountry.SelectedIndex = 0;
}

cmbCountry始终为空!

<ComboBox 
    x:Name="cmbCountry" 
    Grid.Row="3" 
    Grid.Column="1" 
    Grid.ColumnSpan="4" 
    Margin="5" 
    SelectedValuePath="CountryId" 
    Validation.Error="Validation_Error"
    SelectedValue="{Binding Path=CountryId, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" 
    >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding CountryAName}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

0 个答案:

没有答案