【问题标题】:How do I add a command to a non button custom control in WPF for MVVM如何在 WPF for MVVM 中向非按钮自定义控件添加命令
【发布时间】:2021-04-09 22:46:44
【问题描述】:

所以我使用的是用户控件(不是窗口)并且我制作了一个可点击的控件,我没有使用按钮控件。根本没有按钮控件。如何使用命令?这样我就可以将鼠标按下事件视而不见?

我在我的代码后面:

` public partial class PowerButton : UserControl, ICommand
{
    public PowerButton()
    {
        InitializeComponent();
    }

    private bool _powerButtonClick;

    public bool PowerButtonClick
    {
        get { return _powerButtonClick; }
        set { _powerButtonClick = value; }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public event EventHandler CanExecuteChanged;

    private void onPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


    private void UIElement_OnMouseDown(object sender, MouseButtonEventArgs e)
    {
        PowerButtonClick = true;
    }


    public bool CanExecute(object parameter)
    {
        bool temp = false;

        if (PowerButtonClick)
        {
            temp = PowerButtonClick;
            PowerButtonClick = false;

        }

        return temp;
    }

    public void Execute(object parameter)
    {

    }

    private void CommandBinding_OnExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        throw new NotImplementedException();
    }

    private void CommandBinding_OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        throw new NotImplementedException();
    }
}`

这是我的 xmls :

<UserControl x:Class="HmiButtonControl.PowerButton"
         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:ed="clr-namespace:Microsoft.Expression.Shapes;assembly=Microsoft.Expression.Drawing"
         xmlns:local="clr-namespace:HmiButtonControl"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800" >

<UserControl.CommandBindings>
    <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_OnExecuted" CanExecute="CommandBinding_OnCanExecute" />
</UserControl.CommandBindings>

<Viewbox>
    <Canvas Height ="100" Width="100">
        <Ellipse Canvas.Left="0" Canvas.Top="0" Width="100" Height="100" StrokeThickness="2" Stroke ="Gray">
            <Ellipse.Fill>
                <LinearGradientBrush x:Name ="LinearProcessBrush" StartPoint ="0.5, 0.5" EndPoint="1,0" SpreadMethod="Pad">
                    <GradientStop Color ="Black" Offset="0"/>
                    <GradientStop Color="Gray" Offset ="2" />
                </LinearGradientBrush>
            </Ellipse.Fill>
        </Ellipse>

        <Ellipse Canvas.Left="15" Canvas.Top="15" Width="70" Height="70" StrokeThickness="0" Stroke ="Gray" MouseDown="UIElement_OnMouseDown" >
            <Ellipse.Fill>
                <LinearGradientBrush x:Name ="LinearProcessBrush2" StartPoint ="1, 0.5" EndPoint="0,1" SpreadMethod="Pad">
                    <GradientStop Color ="Black" Offset="0"/>
                    <GradientStop Color="Gray" Offset ="2" />
                </LinearGradientBrush>
            </Ellipse.Fill>
        </Ellipse>

        <ed:Arc Canvas.Left="25" Width="50" Height="50" Canvas.Top="25" ArcThickness="6" StartAngle="30" EndAngle="330" Stretch="None" Fill ="Red"  />
        <Rectangle Canvas.Left="46" Canvas.Top="20" Width="8" Height="24" Stretch="Fill" Fill="Red" />
    </Canvas>
</Viewbox>

但是当我尝试和使用这个控件时不允许我使用该命令。 这是为什么 ? 我该如何使用它?我想做一个点击的事情。

【问题讨论】:

    标签: c# wpf wpf-controls


    【解决方案1】:

    我认为您正在寻找的是 ICommand 类型的依赖属性。您可以将下面类似的内容添加到您的 PowerButton 类中。

            public static DependencyProperty PowerButtonCommandProperty
        = DependencyProperty.Register("PowerButtonCommand", typeof(ICommand), typeof(PowerButton));
    
        public ICommand PowerButtonCommand
        {
            get { return (ICommand)GetValue(PowerButtonCommandProperty); }
            set { SetValue(PowerButtonCommandProperty, value); }
        }
    

    【讨论】:

    • @darbid,好的,我添加了代码,现在我可以看到并链接到 PowerButtonCommand。但是当我点击我的控件时如何设置它?我添加了一个 UIElement_OnMouseDown 并且该部分有效,但是当我在该事件中添加代码时?我试过 PowerButtonCommand = new PowerButton 但没用。
    • @Darbid 没关系,我让它工作了!!!谢谢大佬!!!
    猜你喜欢
    • 2011-10-18
    • 2015-06-07
    • 2010-10-10
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多