WPF自定义控件-绑定到在后台代码中定义的命令

463

我正在尝试创建一个名为“ DataTextBox”的WPF自定义控件。除了此控件的上下文菜单之外,其他所有东西都工作正常。确实,我想在DataTextBox的上下文菜单中添加一个项目。为此,我以在generic.xaml中定义的DataTextBox样式添加了一个MenuItem:

<Style TargetType="{x:Type controls:DataTextBox}">
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu>
                <MenuItem Header="{DynamicResource Components_TextBoxCut}" Command="ApplicationCommands.Cut" />
                <MenuItem Header="{DynamicResource Components_TextBoxCopy}" Command="ApplicationCommands.Copy" />
                <MenuItem Header="{DynamicResource Components_TextBoxPaste}" Command="ApplicationCommands.Paste" />
                <MenuItem x:Name="menuItemInsertChecksum" Header="{DynamicResource Components_DataTextBoxInsertChecksum}"
                Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:DataTextBox}}, Path=CalculateChecksumCommand}" />
            </ContextMenu>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
              ...
        </Setter.Value>
    </Setter>
</Style>

我还在代码背后的DataTextBox中添加了一条命令

    public static DependencyProperty CalculateChecksumCommandProperty = DependencyProperty.Register("CalculateChecksumCommand", typeof(ICommand), typeof(DataTextBox));
    public ICommand CalculateChecksumCommand { get; private set; }

此命令在DataTextBox构造函数中初始化:

    public DataTextBox() : base()
    {
        CalculateChecksumCommand = new RelayCommand(() => CalculateChecksum(), () => CanCalculateChecksum());
    }

我的问题是我的最后一个MenuItem的命令绑定不起作用,因为找不到“ CalculateChecksumCommand”。这意味着从不调用“ CalculateChecksum()”方法。

我希望在这个问题上有所帮助。谢谢你。

编辑:依赖项属性声明应为:

    public static DependencyProperty CalculateChecksumCommandProperty = DependencyProperty.Register("CalculateChecksumCommand", typeof(ICommand), typeof(DataTextBox));
    public ICommand CalculateChecksumCommand
    {
        get { return (ICommand)GetValue(CalculateChecksumCommandProperty); }
        private set { SetValue(CalculateChecksumCommandProperty, value); }
    }
艾比

承载控件并为其定义样式的窗口,该窗口将其上下文菜单的一个菜单项绑定到其命令:

XAML

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wpfApplication2="clr-namespace:WpfApplication2"
        Title="MainWindow"
        Width="525"
        Height="350"
        mc:Ignorable="d">
    <Grid>
        <Grid.Resources>
            <Style TargetType="wpfApplication2:UserControl1" x:Shared="False">
                <Style.Setters>
                    <Setter Property="ContextMenu">
                        <Setter.Value>
                            <ContextMenu>
                                <MenuItem Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget.(wpfApplication2:UserControl1.MyCommand)}" Header="Hello" />
                            </ContextMenu>
                        </Setter.Value>
                    </Setter>
                </Style.Setters>
            </Style>
        </Grid.Resources>
        <wpfApplication2:UserControl1 />

    </Grid>
</Window>

代码

using System;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication2
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }


    public class DelegateCommand : ICommand
    {
        private readonly Func<object, bool> _canExecute;
        private readonly Action<object> _execute;

        public DelegateCommand(Action<object> execute) : this(execute, s => true)
        {
        }

        public DelegateCommand(Action<object> execute, Func<object, bool> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        public event EventHandler CanExecuteChanged;
    }
}

控制 :

using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication2
{
    /// <summary>
    ///     Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl, INotifyPropertyChanged
    {
        private DelegateCommand _myCommand;

        public UserControl1()
        {
            InitializeComponent();

            MyCommand = new DelegateCommand(Execute);
        }

        public DelegateCommand MyCommand
        {
            get { return _myCommand; }
            set
            {
                _myCommand = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void Execute(object o)
        {
            MessageBox.Show("Hello");
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

自定义 WPF 控件的好处

来自分类Dev

WPF 样式自定义控件

来自分类Dev

WPF自定义控件:将CollectionViewSource绑定到DependencyProperty

来自分类Dev

WPF:自定义创建的用户控件中的数据绑定

来自分类Dev

WPF KeyBinding 命令未在自定义控件中触发

来自分类Dev

子控件未继承 WPF 自定义控件样式

来自分类Dev

自定义控件数据绑定wpf

来自分类Dev

无法绑定自定义控件内容(WPF)

来自分类Dev

继承的WPF自定义控件不继承父命令

来自分类Dev

WPF中的自定义控件模板的Click事件

来自分类Dev

WPF自定义控件中的依赖关系

来自分类Dev

WPF:如何使自定义控件中的文本显示?

来自分类Dev

WPF-更新自定义控件中的值

来自分类Dev

在WPF MaterialDesign中悬停自定义控件

来自分类Dev

在自定义WPF控件中强制重新绘制自定义绘制的UIElement

来自分类Dev

wpf-无法使用自定义控件

来自分类Dev

WPF自定义控件,模型作为依赖项属性

来自分类Dev

WPF-从自定义ListBoxItem检索子DataTemplate控件

来自分类Dev

WPF自定义控件丢失了其事件订阅

来自分类Dev

最佳WPF面板可创建自定义控件

来自分类Dev

WPF自定义控件不使用ToggleButton的默认样式

来自分类Dev

WPF、自定义控件和样式/主题继承

来自分类Dev

自定义控件,绑定到背后的代码

来自分类Dev

自定义控件,绑定到背后的代码

来自分类Dev

绑定到子自定义控件的控件模板中父用户控件代码中定义的属性

来自分类Dev

绑定到子自定义控件的控件模板中父用户控件代码中定义的属性

来自分类Dev

绑定到wpf自定义控件依赖项属性以获取工具提示吗?

来自分类Dev

绑定到wpf自定义控件依赖项属性以获取工具提示吗?

来自分类Dev

WPF:自定义控件属性已被另一个自定义控件错误注册

Related 相关文章

  1. 1

    自定义 WPF 控件的好处

  2. 2

    WPF 样式自定义控件

  3. 3

    WPF自定义控件:将CollectionViewSource绑定到DependencyProperty

  4. 4

    WPF:自定义创建的用户控件中的数据绑定

  5. 5

    WPF KeyBinding 命令未在自定义控件中触发

  6. 6

    子控件未继承 WPF 自定义控件样式

  7. 7

    自定义控件数据绑定wpf

  8. 8

    无法绑定自定义控件内容(WPF)

  9. 9

    继承的WPF自定义控件不继承父命令

  10. 10

    WPF中的自定义控件模板的Click事件

  11. 11

    WPF自定义控件中的依赖关系

  12. 12

    WPF:如何使自定义控件中的文本显示?

  13. 13

    WPF-更新自定义控件中的值

  14. 14

    在WPF MaterialDesign中悬停自定义控件

  15. 15

    在自定义WPF控件中强制重新绘制自定义绘制的UIElement

  16. 16

    wpf-无法使用自定义控件

  17. 17

    WPF自定义控件,模型作为依赖项属性

  18. 18

    WPF-从自定义ListBoxItem检索子DataTemplate控件

  19. 19

    WPF自定义控件丢失了其事件订阅

  20. 20

    最佳WPF面板可创建自定义控件

  21. 21

    WPF自定义控件不使用ToggleButton的默认样式

  22. 22

    WPF、自定义控件和样式/主题继承

  23. 23

    自定义控件,绑定到背后的代码

  24. 24

    自定义控件,绑定到背后的代码

  25. 25

    绑定到子自定义控件的控件模板中父用户控件代码中定义的属性

  26. 26

    绑定到子自定义控件的控件模板中父用户控件代码中定义的属性

  27. 27

    绑定到wpf自定义控件依赖项属性以获取工具提示吗?

  28. 28

    绑定到wpf自定义控件依赖项属性以获取工具提示吗?

  29. 29

    WPF:自定义控件属性已被另一个自定义控件错误注册

热门标签

归档