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