Added buttons to the application fails to execute commandbinding?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace WPF.Commands { public class DataCommands { public static RoutedCommand Delete { get; set; } public static RoutedCommand Edit { get; set; } static DataCommands() { InputGestureCollection inputs = new InputGestureCollection(); inputs.Add(new KeyGesture(Key.E, ModifierKeys.Control, "Ctrl+E")); Edit = new RoutedCommand("Edit", typeof(DataCommands), inputs); inputs = new InputGestureCollection(); inputs.Add(new KeyGesture(Key.D, ModifierKeys.Control, "Ctrl+D")); Delete = new RoutedCommand("Delete", typeof(DataCommands), inputs); } } } 

Here's the code of the page where I'm trying to add:

 <Page x:Class="WPF.PageEmployee" 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:command="clr-namespace:WPF.Commands" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Title="PageEmployee"> <Page.CommandBindings> <CommandBinding Command="Edit" Executed="EditCommandBinding_Executed" CanExecute="EditCommandBinding_CanExecute" /> </Page.CommandBindings> <StackPanel Margin="3" Background="{StaticResource BackgroundWindowResource}"> <Menu> <MenuItem Header="Действие" > <MenuItem Header="Отменить" ></MenuItem> <Separator></Separator> <MenuItem Header="Создать" ></MenuItem> <MenuItem Header="Редактировать" ></MenuItem> <MenuItem Header="Сохранить" ></MenuItem> <MenuItem Header="Найти" /> <Separator></Separator> <MenuItem Header="Удалить" ></MenuItem> </MenuItem> <MenuItem Header="Отчет"></MenuItem> </Menu> <ToolBar Name="ToolBar1" Margin="3"> <Button Name="Undo" ToolTip="Отменить редактирование/создание" Margin="5,2,5,2"> <Image Source="Images/Undo.png" /> </Button> <Button Name="Save" ToolTip="Сохранить" Margin="5,2,5,2"> <Image Source="Images/Save.png" /> </Button> <Button Name="Find" ToolTip="Найти" Margin="5,2,5,2"> <Image Source="Images/Find.png" /> </Button> <Button Name="Edit" ToolTip="Редактировать" Margin="5,2,5,2"> <Image Source="Images/Edit.png" /> </Button> <Button Name="Delete" ToolTip="Удалить" Margin="5,2,5,2"> <Image Source="Images/Delete.png" /> </Button> <Button Name="Add" ToolTip="Создать" Margin="5,2,5,2"> <Image Source="Images/Add.png" /> </Button> </ToolBar> <TextBlock Margin="5" >Список сотрудников</TextBlock> <DataGrid Name="DataGridEmployee" > <DataGrid.Columns> <DataGridTextColumn Header="Фамилия"/> <DataGridTextColumn Header="Имя"/> <DataGridTextColumn Header="Отчество"/> <DataGridComboBoxColumn Header="Должность" /> <DataGridTemplateColumn Header="Дата рождения" /> <DataGridTextColumn Header="Телефон"/> <DataGridTextColumn Header="Электронная почта"/> </DataGrid.Columns> </DataGrid> </StackPanel> </Page> 
  • And you could not throw out all unnecessary from the code? For example, <TextBlock Margin="5" >Список сотрудников</TextBlock> clearly not needed. Like DataGrid . - VladD
  • Why do I wonder why commandbinding doesn't work - Arseny
  • The less code you have, the more likely it is that a qualified participant will want to spend his time and sort out your problem. ( minimal reproducible example ) - VladD
  • I do not force anyone! - Arseny

1 answer 1

Actually, where is the connection between DataCommands and your Page? I don’t see the context of the Page, and the class is generally static, it’s not very good, although it’s permissible. And where are the bindings? Here is a good example of how to connect.