powered by simpleCommunicator - 2.0.59     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / Commandbinding
5 сообщений из 5, страница 1 из 1
Commandbinding
    #36744693
santyaga
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Добрый день!

Подскажите пожалуйста что я делаю не правильно. Не вызывается событие CanExecuted.
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
<Window x:Class="Task.DynamicXaml1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Task"
    Title="DynamicXaml1" Height="365" Width="535">
    <Window.Resources>
        
        <CommandBinding x:Key="OpenCustom" Command="local:CustomCommand.Open" Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute"/>
        <CommandBinding x:Key="CloseCustom" Command="local:CustomCommand.Close" Executed="CommandBinding_Executed_1" CanExecute="CommandBinding_CanExecute"/>
        
        <ContextMenu x:Key="menu">            
            <MenuItem Header="Open" Command="local:CustomCommand.Open" />
            <MenuItem Header="Close" Command="local:CustomCommand.Close" />
        </ContextMenu>
        
    </Window.Resources>
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Task
{
    public static class CustomCommand
    {
        private static RoutedUICommand open = new RoutedUICommand("Open", "Open", typeof(CustomCommand));
        private static RoutedUICommand close = new RoutedUICommand("Close", "Close", typeof(CustomCommand));

        public static RoutedUICommand Open
        {
            get { return open; }
        }

        public static RoutedUICommand Close
        {
            get { return close; }
        }
    }
}

...
Рейтинг: 0 / 0
Commandbinding
    #36745117
santyaga
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
поменялась проблема. разобрался с canexecute. дело оказалось в свойстве commandtarget. зафиксил это так:
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
        public DynamicXaml1()
        {
            InitializeComponent();

            ResourceDictionary _resources = main1.Resources;
            ContextMenu _contextMenu = (ContextMenu)_resources["menu"];
            ItemCollection _menuItems = f.Items;
            foreach (MenuItem _menuItem in _menuItems)
                _menuItem.CommandTarget = this;
        }

Однако вот в чем вся штука. если прописать это свойство в xaml, оно работать не будет. Это что баг?
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
<Window x:Class="Task.DynamicXaml1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Task"
        x:Name="main"        
    Title="DynamicXaml1" Height="365" Width="535">
    <Window.Resources>       
        
        <ContextMenu x:Key="menu" x:Name="n">            
            <MenuItem x:Name="m1" Header="Open" CommandTarget="{Binding ElementName=main}" Command="{ x:Static local:CustomCommand.Open }" />
            <MenuItem x:Name="m2" Header="Close" CommandTarget="{Binding ElementName=main}" Command="{ x:Static local:CustomCommand.Close }" />
        </ContextMenu>
        
    </Window.Resources>
    
    <Window.CommandBindings>
        <CommandBinding Command="{ x:Static local:CustomCommand.Open }"  Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute" />
        <CommandBinding Command="{ x:Static local:CustomCommand.Close }" Executed="CommandBinding_Executed_1" CanExecute="CommandBinding_CanExecute" />
    </Window.CommandBindings>
    
    <Grid ContextMenu="{DynamicResource menu}">
       ...
    </Grid>
</Window>

...
Рейтинг: 0 / 0
Commandbinding
    #36746320
Bob Dylan
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
santyaga,

In order to use the command in an application, event handlers which define what the command does must be created


private void ExecutedCustomCommand(object sender,
ExecutedRoutedEventArgs e)
{
MessageBox.Show("Custom Command Executed");
}
...
Рейтинг: 0 / 0
Commandbinding
    #36746324
Bob Dylan
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
http://stackoverflow.com/questions/601393/custom-command-wpf
...
Рейтинг: 0 / 0
Commandbinding
    #36746327
Bob Dylan
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
так понимаю, что тут код не полностью приведён,
поэтому остаётся только гадать, что там не так-
если комманд определён в одном елементе, а вызывается из другого,
то пример можно найти тут :

http://stackoverflow.com/questions/1015851/setting-command-target-in-xaml
...
Рейтинг: 0 / 0
5 сообщений из 5, страница 1 из 1
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / Commandbinding
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]