У меня есть два связанных объекта, которые имеют отношение один ко многим.При изменении значения одного из элементов я вижу, что значение в наблюдаемой коллекции изменилось, но в ListView ничего не изменилось.При добавлении и удалении изменения в ListView отображаются.Первая модель:
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; }
}
Вторая модель:
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:
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;
}
// Create Collection of DeviceCategories
private ObservableCollection<device_categories> deviceCategories;
public ObservableCollection<device_categories> DeviceCategories
{
get
{
return deviceCategories;
}
set
{
deviceCategories = value;
}
}
// Create Collection of Device Complexity (used in comboBox)
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;
}
}
//Insert new Item
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();
}
// Modify Item (I think problem somewhere here)
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();
}
}
XAML
<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>
Я вижу изменения в ComboBoxи TextBox, после того как я нажал на кнопку изменения