如何在 WPF 中为 MVVM 添加命令到非按钮自定义控件
How do I add a command to a non button custom control in WPF for MVVM
所以我使用的是用户控件(不是 window)并且我制作了一个可点击的控件,我没有使用按钮控件。根本没有按钮控件。如何使用命令?这样我就可以屏蔽我的鼠标按下事件了吗?
我在后面的代码中是这样的:
` 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 class.
public static DependencyProperty PowerButtonCommandProperty
= DependencyProperty.Register("PowerButtonCommand", typeof(ICommand), typeof(PowerButton));
public ICommand PowerButtonCommand
{
get { return (ICommand)GetValue(PowerButtonCommandProperty); }
set { SetValue(PowerButtonCommandProperty, value); }
}
所以我使用的是用户控件(不是 window)并且我制作了一个可点击的控件,我没有使用按钮控件。根本没有按钮控件。如何使用命令?这样我就可以屏蔽我的鼠标按下事件了吗?
我在后面的代码中是这样的:
` 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 class.
public static DependencyProperty PowerButtonCommandProperty
= DependencyProperty.Register("PowerButtonCommand", typeof(ICommand), typeof(PowerButton));
public ICommand PowerButtonCommand
{
get { return (ICommand)GetValue(PowerButtonCommandProperty); }
set { SetValue(PowerButtonCommandProperty, value); }
}