I have used all day to figure out what happens, and why this errormessage is shown when I link to a view with data from db. But is fine, when only viewing some textstring.
My solution have 5 projects: TheUI, DAL, Model, ViewModel and Views. When run to curser method I can see that the 3 rows from the db goes fine into the Views (UserControls), but when linking on the UI to the specifc UserControl something goes wrong.
I'm pretty sure, there is nothing wrong with DAL and Model, so I'm showing you the 3 other projects.
I'm not cable to attach the file. I do not know why the solution zipped is sice 47mb. If You want code for the other projects, just say so.
The error is in below line in MainWindow.xaml line 110
- <views:CategoryView x:Name="CategoryView" Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="2" />
Best regards
Simsen :)
ViewModel.Account.CategoryViewModel:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections.ObjectModel;
- using Model.Account;
- using DAL.Account;
- namespace ViewModel.Account
- {
- public class CategoryViewModel
- {
- private Category _selectedCategory;
- public MyICommand DeleteCommand { get; set; }
- public Category SelectedCategory
- {
- get
- {
- return _selectedCategory;
- }
- set
- {
- _selectedCategory = value;
- DeleteCommand.RaiseCanExecuteChanged();
- }
- }
- public CategoryViewModel()
- {
- LoadCategories();
- DeleteCommand = new MyICommand(OnDelete, CanDelete);
- }
- public ObservableCollection<Category> Categories { get; set; }
- public void LoadCategories()
- {
- DalCategory dalCategory = new DalCategory();
- ObservableCollection<Category> dalCategories = dalCategory.GetCategories();
- Categories = dalCategories;
- }
- #region Delete
- private void OnDelete()
- {
- Categories.Remove(SelectedCategory);
- }
- private bool CanDelete()
- {
- return SelectedCategory != null;
- }
- #endregion
- }
- }
Views.CategoryView.Account.CategoryView.xaml:
- <UserControl x:Class="Views.Account.CategoryView"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:viewModel = "clr-namespace:ViewModel.Account;assembly=ViewModel"
- xmlns:data = "clr-namespace:Model.Account;assembly=Model"
- mc:Ignorable="d"
- d:DesignHeight="450" d:DesignWidth="800">
- <Grid>
- <StackPanel Orientation = "Horizontal">
- <ListBox ItemsSource = "{Binding Mode=OneWay}">
- <DataTemplate>
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="{Binding CategoryId}" />
- <TextBlock Text="{Binding CategoryName}" />
- <TextBlock Text="{Binding CategoryIsGlobal}" />
- <TextBlock Text="{Binding ProjectId}" />
- <TextBlock Text="{Binding IsObsolete}" />
- </StackPanel>
- </DataTemplate>
- </ListBox>
- </StackPanel>
- </Grid>
- </UserControl>
Views.CategoryView.Account.CategoryView.xaml.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace Views.Account
- {
-
-
-
- public partial class CategoryView : UserControl
- {
- public CategoryView()
- {
- InitializeComponent();
- this.DataContext = new ViewModel.Account.CategoryViewModel();
- string HereICheckTheDataContextAndItHas3Rows = "";
- }
- }
- }
AnsiBug.MainWindow.xaml:
And finally AnsiBug.MainWindow.xaml.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace AnsiBug
- {
-
-
-
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- HideUserControls();
-
- }
- private void HideUserControls()
- {
-
-
- }
- #region Topmenu knapper
- private void BtnLogout_Click(object sender, RoutedEventArgs e)
- {
- this.Close();
- }
- private void BtnTopMenu_Click(object sender, RoutedEventArgs e)
- {
- var button = (Button)sender;
- string btnName = "cm" + button.Name;
- ContextMenu cm = this.FindResource(btnName) as ContextMenu;
- cm.PlacementTarget = sender as Button;
- cm.IsOpen = true;
- }
- #endregion
- }
- }