K
K
krslnkv2019-03-11 18:11:40
C++ / C#
krslnkv, 2019-03-11 18:11:40

Why does the ListView not change after changing the value?

I have two related objects that have a one-to-many relationship. When I change the value of one of the items, I see that the value in the observablecollection has changed, but nothing has changed in the ListView. When adding and removing occur.
Model of the first object:

public partial class device_categories
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public device_categories()
        {
            this.devices = new HashSet<device>();
        }
    
        public int device_category_id { get; set; }
        public string category_name { get; set; }
        public int device_complexity_id { get; set; }
    
        public virtual device_complexities device_complexities { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<device> devices { get; set; }
    }

Model of the second object
public partial class device_complexities
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public device_complexities()
        {
            this.device_categories = new HashSet<device_categories>();
        }
    
        public int device_complexity_id { get; set; }
        public string device_complexity_name { get; set; }
    
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<device_categories> device_categories { get; set; }
    }

ViewModel (collection itself, add/change method and selected object)
class DeviceCategoriesViewModel
    {
        newtechd dc = new newtechd();
        public DeviceCategoriesViewModel()
        {
            dc.device_categories.Include(com => com.device_complexities).Load();
            dc.device_complexities.Load();
            DeviceCategories = dc.device_categories.Local;
            DeviceComplexities = dc.device_complexities.Local;
        }

        private ObservableCollection<device_categories> deviceCategories;
        public ObservableCollection<device_categories> DeviceCategories
        {
            get
            {
                return deviceCategories;
            }

            set
            {
                deviceCategories = value;
            }
        }

        private ObservableCollection<device_complexities> deviceComplexities;
        public ObservableCollection<device_complexities> DeviceComplexities
        {
            get
            {
                return deviceComplexities;
            }

            set
            {
                deviceComplexities = value;
            }
        }

        private device_complexities selectedDeviceComplexity;
        public device_complexities SelectedDeviceComplexity
        {
            get
            {
                return selectedDeviceComplexity;
            }

            set
            {
                selectedDeviceComplexity = value;
            }
        }

        private device_categories selectedDeviceCategory;
        public device_categories SelectedDeviceСategory
        {
            get
            {
                return selectedDeviceCategory;
            }

            set
            {
                selectedDeviceCategory = value;
            }
        }

        public void AddDevCat(string name)
        {
            string addName = (String)name;

            device_categories add_Device_Categories = new device_categories
            {
                category_name = addName,
                device_complexity_id = selectedDeviceComplexity.device_complexity_id
            };
            dc.device_categories.Add(add_Device_Categories);
            dc.SaveChanges();
        }

        public void EditDevCat(string newName, int comp_id)
        {
            string newCatName = (String)newName;
            int dev_comp_id = Convert.ToInt32(comp_id);

            device_categories dev_cat_to_edit = selectedDeviceCategory;
            device_complexities new_dev_comp = dc.device_complexities.Find(dev_comp_id);
            dev_cat_to_edit.category_name = newCatName;
            dev_cat_to_edit.device_complexities = new_dev_comp;
            dc.Entry(dev_cat_to_edit).State = EntityState.Modified;
            dc.SaveChanges();
            ObservableCollection<device_categories> device_CategoriesModifed = dc.device_categories.Local;
            DeviceCategories = device_CategoriesModifed;
        }
    }

XAML ListView
<ListView x:Name="deviceCategoriesLV" HorizontalAlignment="Right" MaxHeight="200" MinHeight="150"  Margin="0,15,411.6,0" Grid.Column="0" ItemsSource="{Binding DeviceCategories}" SelectedItem="{Binding SelectedDeviceСategory}" VerticalAlignment="Top" Width="372" Height="226">
            <ListBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Удалить" x:Name="deleteDeviceCategory" Click="DeleteDeviceCategory_Click"/>
                </ContextMenu>
            </ListBox.ContextMenu>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="5">
                        <TextBlock FontSize="14" Text="{Binding Path=category_name}" />
                        <TextBlock Text="{Binding Path=device_complexities.device_complexity_name}" />
                        <!--<TextBlock FontSize="18" Text="{Binding Path=Brand_name}" />-->
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question