Help with Final project app

Eduardo Gomez 3,196 Reputation points
2021-05-13T18:35:07.497+00:00

Hello guys

I am building a Notepad app (with more functionality), The idea is, that I will have a list of notebooks and notes associated with those notebooks.

Whenever I created a notebook, a cannot create a new note, because the selected notebook is null,

this is my Notes VM

      public ICommand ExitCommand { get; set; }
        public ICommand NewNotebookCommand { get; set; }
        public ICommand NewNoteCommand { get; set; }

        public ObservableCollection<Notebook> Notebooks { get; set; } = new ObservableCollection<Notebook>();

        public ObservableCollection<Note> Notes { get; set; } = new ObservableCollection<Note>();

        private Notebook _SelectedNoteBook;
        public Notebook SelectedNoteBook {
            get { return _SelectedNoteBook; }
            set {
                if (_SelectedNoteBook != value) {
                    _SelectedNoteBook = value;
                    RaisePropertyChanged();
                    GetNotes();
                }
            }
        }


        public NotesWindowVM() {

            ExitCommand = new Command(() => {
                Application.Current.Shutdown();
            });
            NewNotebookCommand = new Command(() => {
                CreateNewNotebook();
            });
            NewNoteCommand = new HelperCommand {
                ExecuteDelegate = x => CreateNewNote(SelectedNoteBook.Id),
                CanExecuteDelegate = x => SelectedNoteBook != null
            };

            GetNoteBooks();
        }

        private void CreateNewNote(int NotebookId) {
            Note note = new() { NotebookId = NotebookId, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, Title = "New Note" };
            Database.Insert(note);

            GetNotes();
        }

        private void GetNoteBooks() {

            var notebooks = Database.Read<Notebook>();

            Notebooks.Clear();
            foreach (var item in notebooks) {
                Notebooks.Add(item);
            }
        }

        private void GetNotes() {

            if (SelectedNoteBook != null) {
                var notes = Database.Read<Note>().Where(n => n.NotebookId == SelectedNoteBook.Id).ToList();

                Notebooks.Clear();
                foreach (var item in notes) {
                    Notes.Add(item);
                }
            }
        }

        private void CreateNewNotebook() {
            Notebook notebook = new() { Name = "New Notebook" };
            Database.Insert(notebook);

            GetNoteBooks();
        }

    }
}

My DB

public class Database {

    private static string dbFile = Path.Combine(Environment.CurrentDirectory, "NotesDB.db3");

    public static bool Insert<T>(T Item) {

        bool inserted = false;

        using (SQLiteConnection conn = new(dbFile)) {

            conn.CreateTable<T>();
            int row = conn.Insert(Item);
            if (row > 0) {
                inserted = true;
            }
        }
        return inserted;
    }

    public static List<T> Read<T>() where T : new() {

        List<T> items;

        using (SQLiteConnection conn = new(dbFile)) {

            conn.CreateTable<T>();
            items = conn.Table<T>().ToList();
        }
        return items;
    }

    public static bool Update<T>(T Item) {

        bool inserted = false;

        using (SQLiteConnection conn = new(dbFile)) {

            conn.CreateTable<T>();
            int row = conn.Update(Item);
            if (row > 0) {
                inserted = true;
            }
        }
        return inserted;
    }

    public static bool Delete<T>(T Item) {

        bool inserted = false;

        using (SQLiteConnection conn = new(dbFile)) {

            conn.CreateTable<T>();
            int row = conn.Delete(Item);
            if (row > 0) {
                inserted = true;
            }
        }
        return inserted;
    }
}

}

and UI

<Window.DataContext>
<viewmodel:NotesWindowVM />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

    <Fluent:Ribbon>
        <Fluent:RibbonTabItem Header="File">
            <Fluent:RibbonGroupBox Header="Group">
                <Fluent:Button Header="New Notebook"
                               Command="{Binding NewNotebookCommand}"
                               LargeIcon="/Resources/diary.png" />
                <Fluent:Button Header="New Note"
                               Command="{Binding NewNoteCommand}"
                               CommandParameter="{Binding SelectedNoteBook, Mode=TwoWay}"
                               LargeIcon="/Resources/post-it.png" />
                <Fluent:Button Header="Exit"
                               Margin="40,0,0,0"
                               LargeIcon="/Resources/exit.png"
                               Command="{Binding ExitCommand}" />
            </Fluent:RibbonGroupBox>
        </Fluent:RibbonTabItem>
    </Fluent:Ribbon>
    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="140" />
            <ColumnDefinition Width="140" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <ListView Background="#770000EF"
                  ItemsSource="{Binding Notebooks}"
                  SelectedItem="{Binding SelectedNoteBook, Mode=TwoWay}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" 
                               Foreground="#C15237" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <ListView Grid.Column="1"
                  Background="#504DB0B0"
                  ItemsSource="{Binding Notes}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Title}"
                               Foreground="#C15237" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <ToolBarTray Grid.Column="2"
                     VerticalAlignment="Top">
            <ToolBar>
                <Button Content="Speech"/> 
            </ToolBar>
        </ToolBarTray>
    </Grid>
</Grid>

</Window>

Thanks for the help

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,633 questions
{count} votes

Accepted answer
  1. DaisyTian-1203 11,611 Reputation points
    2021-05-14T01:38:17.877+00:00

    You use Notebooks.Clear(); in GetNotes() (Line 60 of the first part of the code), GetNotes() is used in SelectedNoteBook set part.
    When you select a Notebook, project will invoke Notebooks.Clear(); to clear the Notebooks collection. Please change Notebooks.Clear(); to Notes.Clear(); .


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful