【问题标题】:How to bind ObservableCollection to ListView?如何将 ObservableCollection 绑定到 ListView?
【发布时间】:2019-02-19 11:53:53
【问题描述】:

我创建了一个 ObservableCollection 来使用实体框架从数据库中填充 ListViev。当我调用填充方法时,我最终会无休止地尝试填充集合。我有一个模型 Device_compiexity 和 Device_category,它们由 device_complexity_id 关联。我为他们创建了 VievModel,并在此基础上创建了一个已填充的集合。

我的视图模型:

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>

【问题讨论】:

  • 如果在程序执行期间不向集合中添加或删除元素,那么拥有 ObservableCollection 是没有意义的。由于您在 FillDeviceCategories 中创建了一个新集合,因此只需使用普通列表,将 DeviceCategories 属性设置为可写。还要让您的 DeviceCategoryViewModel 实现 INotifyPropertyChanged 接口,并在 DeviceCategories 属性和 SelectedDeviceCategory 属性更改其值时触发其 PropertyChanged 事件。
  • 除此之外,您有一个 DeviceCategoryViewModel 集合作为同一类的属性,这看起来很奇怪。这应该是某种层次结构吗?
  • @Clemens,我创建了这个类来将 DeviceComplexity 加入到 DeviceCategory。也许我做错了什么:)
  • @Eugene:“无尽的尝试”是什么意思?您的 FillDeviceCategories() 方法是否被调用? q 是否包含任何项目?如果你打电话给q.ToArray()怎么办?
  • @mm8,是的,FillDeviceCategory 调用无穷无尽,q 有项目

标签: c# wpf entity-framework data-binding observablecollection


【解决方案1】:

当您新建每个 devicecategoryviewmodel 时:

                select new DeviceCategoryViewModel
                {

它的构造函数将运行。 它的构造函数是做什么的?

public DeviceCategoryViewModel()
{
    FillDeviceCategories();
}

那是你的问题。

因为 FillDeviceCategories 中有那个 linq,它反过来会为它找到的每个调用 FillDeviceCategories.... 而每个调用 FillDevicategories... 可能不会完全无休止,因为你最终会堆栈溢出或一些讨厌的东西。

【讨论】:

  • 我需要做些什么来避免这种情况?
  • 也许我应该创建另一个类?
猜你喜欢
  • 2012-05-26
  • 2018-05-21
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
  • 1970-01-01
相关资源
最近更新 更多