C# 如何将WPF中的外键值加载到组合框中
我有一个datatemplate控件,它是基于EF数据集合生成的,该集合被设置为该控件的Datacontext。模板中还有一个组合框,可以将外键值更改为另一个EF entityobject 我正在尝试从viewModel加载外键集合C# 如何将WPF中的外键值加载到组合框中,c#,wpf,entity-framework,combobox,foreign-keys,C#,Wpf,Entity Framework,Combobox,Foreign Keys,我有一个datatemplate控件,它是基于EF数据集合生成的,该集合被设置为该控件的Datacontext。模板中还有一个组合框,可以将外键值更改为另一个EF entityobject 我正在尝试从viewModel加载外键集合 <my:EntryControlBase.Resources> <my:ModulesViewModel x:Key="viewModel"/> </my:EntryControlBase.Resources> 这是
<my:EntryControlBase.Resources>
<my:ModulesViewModel x:Key="viewModel"/>
</my:EntryControlBase.Resources>
这是datatemplate中的控件
<DataTemplate>
<Grid Name="grdRoles" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<TextBox Height="23" Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
*<ComboBox Height="25" ItemsSource="{Binding Source={StaticResource viewModel}, Path=ModulesCollection}"* >
</ComboBox>
</Grid>
</DataTemplate>
*
以及背后的viewmodel代码
public class ModulesViewModel :DependencyObject
{
public ModulesViewModel()
{
using(var context =new SPIS_Entities())
{
ModulesCollection = context.Module.Where(p => p.active).ToList();
}
}
public List<Module> ModulesCollection
{
get
{
return (List<Module>)GetValue(ModulesCollectionProperty);
}
set
{
SetValue(ModulesCollectionProperty, value);
}
}
public static readonly DependencyProperty ModulesCollectionProperty =
DependencyProperty.Register("ModulesCollection",
typeof(List<Module>),
typeof(ModulesViewModel),
null);
}
公共类ModulesViewModel:DependencyObject
{
公共模块视图模型()
{
使用(var context=new SPIS_Entities())
{
moduleCollection=context.Module.Where(p=>p.active.ToList();
}
}
公共列表模块集合
{
得到
{
返回(列表)GetValue(ModuleCollectionProperty);
}
设置
{
SetValue(ModuleCollectionProperty,value);
}
}
公共静态只读DependencyProperty ModulesCollectionProperty=
DependencyProperty.Register(“ModuleCollection”,
类型(列表),
类型(模块视图模型),
无效);
}
我在网上看到了一些例子,但我一直都有一个问题,就是找不到“viewModel”viewModel。它被设置为null
viewmodel的XAML定义设置为usercontrol的根。无论我把它放在哪里,它都有下划线和null,在鼠标上方说“无法创建内部状态…”。如果我把它放在组合框标记中,它仍然是空的,但没有下划线
建议?在我的解决方案中,我有一个非常简单的代码行。我为外键值添加了一个ListProperty,并在我的业务类的loadData()方法中填充了这个列表
private void LoadData(ef.table data)
{
// method to set all the properties
//this.example = data.example;
LoadProperty<ForeignKeyClass>(ForeignKeyProperty, ForeignKeyClass.Get(data.efForeignKeyTable));
}
//Property
public static readonly PropertyInfo<ForeignKeyClass> ForeignKeyProperty = RegisterProperty<ForeignKeyClass>(c => c.ForeignKeyPropertyName);
public ForeignKeyClass ForeignKeyPropertyName
{
get { return GetProperty<ForeignKeyClass>(ForeignKeyProperty); }
private set
{
SetProperty<ForeignKeyClass>(ForeignKeyProperty, value);
}
}
private void LoadData(ef.table数据)
{
//方法来设置所有属性
//this.example=data.example;
LoadProperty(ForeignKeyProperty,ForeignKeyClass.Get(data.efForeignKeyTable));
}
//财产
公共静态只读属性Info ForeignKeyProperty=RegisterProperty(c=>c.ForeignKeyPropertyName);
public ForeignKeyClass ForeignKeyPropertyName
{
get{return GetProperty(ForeignKeyProperty);}
专用设备
{
SetProperty(ForeignKeyProperty,值);
}
}
现在,您已经获得了一个属性,该属性在您的模型中填充了foreignKey数据。我不知道这是否对您有帮助,但是,这就是我如何将我的foreignKey数据输入到我的模型中的方法。祝你好运我也遇到了类似的问题,看看这个帖子-希望这有助于解决你的问题!:)你把getEmp方法放在哪里。我没有看到ViewModel的代码。在您的ViewModel中,在您的示例中是ModulesViewModel。在这个问题中,有两个联系。仔细查看它们,因为它们可能会在实现您想要实现的目标方面为您提供进一步的帮助:)。希望这有帮助。我的问题是我的ViewModel没有被初始化。您使用的是mvvm模式,对吗?