Rename te each note book separately

Eduardo Gomez 3,426 Reputation points
2021-05-16T22:03:08.82+00:00

Hello

I have a list of notebooks and notes. I also have a textbox to rename them, the problem that I have is that when I click rename, the textbox appears on all of them

Ideally what I want is to have a rename button a rename all button.

The rename all activates a textbox for all the notebooks

but the button rename will activate a textbox for the selected notebook

            </ListView>
            <ListView Grid.Column="1"
                      Background="#00007A"
                      ItemsSource="{Binding Notes}"
                      SelectedItem="{Binding SelectedNote}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <controls:CustomNoteControl Note="{Binding}">
                                <controls:CustomNoteControl.ContextMenu>
                                    <ContextMenu>
                                        <MenuItem Header="Rename"
                                                  Command="{Binding Source={StaticResource vm}, Path=RenameCommand}">
                                            <MenuItem.Icon>
                                                <Image Source="/Resources/rename.png" />
                                            </MenuItem.Icon>
                                        </MenuItem>
                                    </ContextMenu>
                                </controls:CustomNoteControl.ContextMenu>
                            </controls:CustomNoteControl>
                            <TextBox Text="{Binding Title}"
                                     FontWeight="Bold"
                                     Visibility="{Binding Source={StaticResource vm}, Path=IsVisible}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="LostFocus">
                                        <i:InvokeCommandAction Command="{Binding Source={StaticResource vm},
                                            Path=RenameNoteCompleteCommand}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </TextBox>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

VM

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


    private bool _IsEnabled;
    public bool IsEnabled {
        get { return _IsEnabled; }
        set {
            if (_IsEnabled != value) {
                _IsEnabled = value;
                RaisePropertyChanged();
            }
        }
    }


    private Note _SelectedNote;
    public Note SelectedNote {
        get { return _SelectedNote; }
        set {
            if (_SelectedNote != value) {
                _SelectedNote = value;
                RaisePropertyChanged();
                if (SelectedNote != null) {
                    IsEnabled = true;
                } else {
                    IsEnabled = false;
                }
            }
        }
    }


    private Visibility _IsVisible;
    public Visibility IsVisible {
        get { return _IsVisible; }
        set {
            if (_IsVisible != value) {
                _IsVisible = value;
                RaisePropertyChanged();
            }
        }
    }

    public NotesWindowVM() {

        IsEnabled = false;
        IsVisible = Visibility.Collapsed;
        Notes = new ObservableCollection<Note>();
        Notebooks = new ObservableCollection<Notebook>();
        Fonts = System.Windows.Media.Fonts.SystemFontFamilies.OrderBy(f => f.Source);
        FontSizes = new List<int>();
        for (int i = 7; i < 72; i++) {
            FontSizes.Add(i);
        }
        ExitCommand = new Command(() => {
            Application.Current.Shutdown();
        });
        NewNotebookCommand = new Command(() => {
            CreateNewNotebook();
        });
        RenameCommand = new Command(() => {
            IsVisible = Visibility.Visible;
        });
        NewNoteCommand = new HelperCommand {
            ExecuteDelegate = x => CreateNewNote(SelectedNoteBook.Id),
            CanExecuteDelegate = x => SelectedNoteBook != null
        };
        RenameNotebookCompleteCommand = new HelperCommand {
            ExecuteDelegate = x => EditionCompltedNotebook(SelectedNoteBook),
            CanExecuteDelegate = x => true
        };
        RenameNoteCompleteCommand = new HelperCommand {
            ExecuteDelegate = x => EditionCompltedNote(SelectedNote),
            CanExecuteDelegate = x => true
        };


        GetNoteBooks();
    }

    private void EditionCompltedNote(Note selectedNote) {
        if (selectedNote != null) {
            IsVisible = Visibility.Collapsed;
            Database.Update(selectedNote);
            GetNoteBooks();
        }
    }

    private void EditionCompltedNotebook(Notebook notebook) {
        if (notebook != null) {
            IsVisible = Visibility.Collapsed;
            Database.Update(notebook);
            GetNotes();
        }

    }

    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 CreateNewNotebook() {
        Notebook notebook = new() { Name = $"Notebook" };
        Database.Insert(notebook);
        GetNoteBooks();
    }

    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();

            Notes.Clear();
            foreach (var item in notes) {
                Notes.Add(item);
            }
        }
    }
}
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,784 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.