我创建了一个ObservableCollection,使用实体框架从数据库中填充ListViev。当我调用fill方法时,我最终无休止地尝试填充集合。我有一个模型Device_compiexity和Device_category,它们与device_complexity_id相关。对于他们,我创建了VievModel,在此基础上我创建了一个填充的集合。
我的ViewModel:
class DeviceCategoryViewModel
{
TechDContext dc = new TechDContext();
public int Device_category_id { get; set; }
public string Сategory_name { get; set; }
public int Device_complexity_id { get; set; }
public string Device_complexity_name { get; set; }
public static DeviceCategoryViewModel DeviceCaterogyVM(DeviceCategory deviceCategory, DeviceComplexity deviceComplexity)
{
return new DeviceCategoryViewModel
{
Device_category_id = deviceCategory.Device_category_id,
Сategory_name = deviceCategory.Category_name,
Device_complexity_id = deviceCategory.Device_complexity_id,
Device_complexity_name = deviceComplexity.Device_complexity_name,
};
}
public DeviceCategoryViewModel()
{
FillDeviceCategories();
}
public void FillDeviceCategories()
{
using (TechDContext dc = new TechDContext())
{
var q = from cat in dc.DeviceCategories
join com in dc.DeviceComplexities on cat.Device_complexity_id equals com.Device_complexity_id
select new DeviceCategoryViewModel
{
Device_category_id = cat.Device_category_id,
Сategory_name = cat.Category_name,
Device_complexity_id = com.Device_complexity_id,
Device_complexity_name = com.Device_complexity_name
};
deviceCategories = new ObservableCollection<DeviceCategoryViewModel>(q);
}
}
private ObservableCollection<DeviceCategoryViewModel> deviceCategories;
public ObservableCollection<DeviceCategoryViewModel> DeviceCategories
{
get
{
return deviceCategories;
}
}
private static DeviceCategoryViewModel selectedDeviceCategory;
public DeviceCategoryViewModel SelectedDeviceCategory
{
get
{
return selectedDeviceCategory;
}
set
{
selectedDeviceCategory = value;
}
}
}
在窗口初始化我做:
DeviceCategoriesPanel.DataContext = new DeviceCategoryViewModel();
在XAML中我这样做:
<Grid HorizontalAlignment="Left" Height="447" x:Name="DeviceCategoriesPanel" Margin="392,2,0,0" VerticalAlignment="Top" Width="392">
<Label x:Name="label1_Copy2" Content="Категории устройств" HorizontalAlignment="Left" Margin="10,0,0,410" VerticalAlignment="Bottom" FontWeight="Bold" Width="188" FontSize="14"/>
<ListView x:Name="categoriesComponentsLV" HorizontalAlignment="Right" MaxHeight="200" MinHeight="150" Margin="0,44,10,0" Grid.Column="0" ItemsSource="{Binding DeviceCategories}" SelectedItem="{Binding SelectedDeviceCategory}" VerticalAlignment="Top" Width="372" Height="197">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="5">
<TextBlock FontSize="18" Text="{Binding Path=Category_name}" />
<TextBlock Text="{Binding Path=Device_complexity_name}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
当您新建每个devicecategoryview模型时:
select new DeviceCategoryViewModel
{
它的构造函数将运行。它的构造函数是做什么的?
public DeviceCategoryViewModel()
{
FillDeviceCategories();
}
那是你的问题。
因为FillDeviceCategories中有linq,它反过来会为它找到的每一个调用FillDeviceCategories ....反过来每次调用FillDevicategories ......可能不完全无穷无尽,因为你最终会堆叠溢出或者令人讨厌的东西。