C# 如果值为true,则添加MouseMove事件

C# 如果值为true,则添加MouseMove事件,c#,wpf,button,mouseevent,C#,Wpf,Button,Mouseevent,我的ItemsControl有DataTemplate选择器,我想实现如下功能: <DataTemplate x:Key="buttonTemplate"> <Button if(someValue = true -> add thisPreviewMouseUp="button_MouseUp") PreviewMouseLeftButtonDown="button_MouseLeftButtonUp" PreviewMouseM

我的ItemsControl有DataTemplate选择器,我想实现如下功能:

        <DataTemplate x:Key="buttonTemplate">
            <Button if(someValue = true -> add thisPreviewMouseUp="button_MouseUp") PreviewMouseLeftButtonDown="button_MouseLeftButtonUp" PreviewMouseMove="button_MouseMove" Click="b_Click">
                <Button.Content>
                    <Image Source="sample.png" Height="{Binding height}" Width="{Binding width}" Stretch="Fill" HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Button.Content>
                <Button.RenderTransform>
                    <RotateTransform Angle="{Binding angle}" />
                </Button.RenderTransform>
            </Button>
        </DataTemplate>

添加此PreviewMouseUp=“button\u MouseUp”)PreviewMouseLeftButtonDown=“button\u MouseLeftButtonUp”PreviewMouseMove=“button\u MouseMove”Click=“b\u Click”>
用户可以从管理器模式移动、更改按钮大小,但我不想在正常模式下触发此事件(现在在鼠标移动事件中有if(\u fromWhere==“manager”)

你知道我该怎么做吗


谢谢

我不知道,在XAML中是否可以设置要触发的事件

但是,您可以在按钮中设置
someValue
,然后在激发事件中按照中的建议检查标记


编辑1:

如果someValue是button的DataContext的一部分,另一种方法是获取button的DataContext:

    var dataContext = ((Button)e.OriginalSource).DataContext;
    if (dataContext.someValue) 
    {
        ...
    }

编辑2:

经过一点调查,我发现可以根据
someValue
添加MouseMove事件。不幸的是,我认为这是不必要的复杂

     <Button Style="{Binding someValue, Converter={StaticResource BoolToButtonStyleConverter}, ConverterParameter={StaticResource MousePreviewEventSetStyle}, Mode=OneWay}" ...>
最后设置事件的处理程序:

    void PreviewMouseUpClicked(object sender, RoutedEventArgs e)
    {
        ...
    }
备注:可能有更好的方法将“默认样式”设置为定义“清除样式”的按钮。

可能会有所帮助

Mvvm方法

我认为这样做很简单,只在条件为真时启用事件

当事件发生时,我在这里执行一个名为DropUser的命令。我使用mvvmlight以便于指挥。您可以使用ICommand Wpf默认命令

xmlns:interactivity=”http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:mvvmlight=”http://www.galasoft.ch/mvvmlight"
视图模型

 private RelayCommand<RoutedEventArgs> _commandName;

    public RelayCommand<RoutedEventArgs>CommandName
    {
        get
        {
            return commandName ?? (commandName = new RelayCommand<RoutedEventArgs>(invokeAction, canExecuteMethod));
        }


    }

    private bool canExecuteMethod()
    {
          check your condition return true
    }

    private void invokeAction(RoutedEventArgs event)
    {
        action you want to do 
    }
private RelayCommand\u命令名;
公共RelayCommandCommandName
{
收到
{
返回commandName??(commandName=newrelaycommand(invokeAction,canExecuteMethod));
}
}
私有布尔canExecuteMethod()
{
检查您的情况并返回true
}
私有void调用(RoutedEventArgs事件)
{
你想做什么
}
    <l:BoolToButtonStyleConverter x:Key="BoolToButtonStyleConverter" />
    <Style x:Key="MousePreviewEventSetStyle" TargetType="{x:Type Button}" >
        <EventSetter Event="PreviewMouseUp" Handler="PreviewMouseUpClicked" />
    </Style>
    <Style x:Key="clearStyle" TargetType="{x:Type Button}" />
    public class BoolToButtonStyleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            bool someValue = (bool)value;

            if (someValue)
                return parameter;
            else
                return Application.Current.Resources["clearStyle"];
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    } 
    void PreviewMouseUpClicked(object sender, RoutedEventArgs e)
    {
        ...
    }
xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:mvvmlight="http://www.galasoft.ch/mvvmlight"


    <interactivity:Interaction.Triggers>
                                    <interactivity:EventTrigger EventName="Drop">
                                        <mvvmlight:EventToCommand 
                                            Command="{Binding DataContext.DropUser, 
                                            PassEventArgsToCommand="True"/>
                                    </interactivity:EventTrigger>
                                </interactivity:Interaction.Triggers>
 #region RelayCommand

    private RelayCommand<DragEventArgs> _dropUser;
    public RelayCommand<DragEventArgs> DropUser
    {
        get
        {
           return _dropUser ?? (_dropUser = new RelayCommand<DragEventArgs>(DropMethod,canExecute));
        }

    }

    private bool canExecute(DragEventArgs arg)
    {
        // check your condition return true . Command is only work when you return true.
    }

    #endregion

 // Method Will Fire here and do action here
 private void DropMethod(DragEventArgs eventArgs)
    {
        if (eventArgs != null)
        {
        }
    }  
<interactivity:Interaction.Triggers>
                                    <interactivity:EventTrigger EventName="PreviewMouseLeftButtonDown">
                                        <mvvmlight:EventToCommand 
                                            Command="{Binding CommandName, 
                                            PassEventArgsToCommand="True"/>
                                    </interactivity:EventTrigger>
                                </interactivity:Interaction.Triggers>
 private RelayCommand<RoutedEventArgs> _commandName;

    public RelayCommand<RoutedEventArgs>CommandName
    {
        get
        {
            return commandName ?? (commandName = new RelayCommand<RoutedEventArgs>(invokeAction, canExecuteMethod));
        }


    }

    private bool canExecuteMethod()
    {
          check your condition return true
    }

    private void invokeAction(RoutedEventArgs event)
    {
        action you want to do 
    }