C# 如何在WPF for MVVM中向非按钮自定义控件添加命令
所以我使用的是一个用户控件(不是窗口),我制作了一个可点击控件,我没有使用按钮控件。根本没有按钮控件。如何使用命令?这样我就可以看不见我的鼠标了 我在我的代码隐藏中添加了以下内容:C# 如何在WPF for MVVM中向非按钮自定义控件添加命令,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,所以我使用的是一个用户控件(不是窗口),我制作了一个可点击控件,我没有使用按钮控件。根本没有按钮控件。如何使用命令?这样我就可以看不见我的鼠标了 我在我的代码隐藏中添加了以下内容: ` public partial class PowerButton : UserControl, ICommand { public PowerButton() { InitializeComponent(); } private bool _powerButton
` 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();
}
}`
这是我的XML:
<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>
但当我尝试使用此控件时,它不允许我使用该命令。
为什么呢?
我如何使用它?我想做一个点击操作。我想你要找的是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。但是当我点击我的控件时,我该如何设置它呢?我在mousedown上添加了一个UIElement_,该部分就可以工作了,但是什么时候我要在该事件旁边添加代码呢?我试过PowerButtonCommand=新的PowerButton,但没用。@Darbid别担心,我成功了!!!谢谢你,伙计!!!