如何写MVVM WPF(C#)这些特定行?

问题描述 投票:3回答:2

.

   Console.WriteLine(DateTime.Now.ToLongTimeString());
            string name = (sender as Button).Name;

我有以下两行MVVM。下面是我的代码转换。

ViewModel.cs:

       public ICommand MouseEnterCommand
                {
                    get
                    {
                        return new RelayCommand(a => this.Executemethod(), p => Canexecutemethod());
                    }
                }
                public bool Canexecutemethod()
                {
                    return true;
                }

                public void Executemethod(){
            Console.WriteLine(DateTime.Now.ToLongTimeString());
            string name = (sender as Button).Name;
switch(name)
            {
case "btn1":
...
case "btn2":
            ...
          }}

凡BTN1和BTN2是按钮名称.....我有四个按钮完全

View.xaml ::

   <UserControl
    xmlns:interact="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    ..>
    <Grid>
    <Button Name="btn1">
    <interact:Interaction.Triggers>
    <interact:EventTrigger EventName="MouseEnter">
    <interact:InvokeCommandAction Command="{Binding  Path=DataContext.ZoomInCommand}" CommandParameter="{Binding }" />
    </interact:EventTrigger>
    </interact:Interaction.Triggers>
    </Button>
    </Grid>
    </UserControl>

请帮我写MVVM..Thanks这些线路提前

c# mvvm mouseenter
2个回答
0
投票

在这里,您将需要EventToCommandBehavior将转移Click事件您的活动参数viewModel。像这样:

/// <summary>
/// Behavior that will connect an UI event to a viewmodel Command,
/// allowing the event arguments to be passed as the CommandParameter.
/// </summary>
public class EventToCommandBehavior : Behavior<FrameworkElement>
{
    // Event
    public string Event
    {
        get { return ( string ) GetValue( EventProperty ); }
        set { SetValue( EventProperty, value ); }
    }

    public static readonly DependencyProperty EventProperty =
        DependencyProperty.Register( nameof( Event ), typeof( string ), typeof( EventToCommandBehavior ),
            new PropertyMetadata( null, OnEventChanged ) );

    // Command
    public ICommand Command
    {
        get { return ( ICommand ) GetValue( CommandProperty ); }
        set { SetValue( CommandProperty, value ); }
    }

    public static readonly DependencyProperty CommandProperty
        = DependencyProperty.Register( nameof( Command ), typeof( ICommand ), typeof( EventToCommandBehavior ), new PropertyMetadata( null ) );

    // PassArguments (default: false)
    public bool PassArguments
    {
        get { return ( bool ) GetValue( PassArgumentsProperty ); }
        set { SetValue( PassArgumentsProperty, value ); }
    }

    public static readonly DependencyProperty PassArgumentsProperty
        = DependencyProperty.Register( nameof( PassArguments ), typeof( bool ), typeof( EventToCommandBehavior ), new PropertyMetadata( false ) );

    protected override void OnAttached()
    {
        AttachHandler( Event ); // initial set
    }

    /// <summary>
    /// Attaches the handler to the event
    /// </summary>
    private void AttachHandler( string eventName )
    {
        // detach old event
        if ( oldEvent != null )
            oldEvent.RemoveEventHandler( AssociatedObject, handler );

        // attach new event
        if ( !string.IsNullOrEmpty( eventName ) )
        {
            EventInfo ei = AssociatedObject.GetType().GetEvent( eventName );
            if ( ei != null )
            {
                MethodInfo mi = GetType().GetMethod( nameof( ExecuteCommand ), BindingFlags.Instance | BindingFlags.NonPublic );
                if ( mi != null )
                {
                    handler = Delegate.CreateDelegate( ei.EventHandlerType, this, mi );
                    ei.AddEventHandler( AssociatedObject, handler );
                    oldEvent = ei; // store to detach in case the Event property changes
                }
                else
                {
                    throw new InvalidOperationException(
                        $"Method {nameof( ExecuteCommand )} not found in class {nameof( EventToCommandBehavior )}" );
                }
            }
            else
            {
                throw new ArgumentException( $"The event '{eventName}' was not found on type '{AssociatedObject.GetType().Name}'" );
            }
        }
    }

    private static void OnEventChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
    {
        var beh = ( EventToCommandBehavior ) d;

        if ( beh.AssociatedObject != null ) // is not yet attached at initial load
            beh.AttachHandler( ( string ) e.NewValue );
    }

    /// <summary>
    /// Executes the Command
    /// </summary>
    // ReSharper disable once UnusedParameter.Local
    private void ExecuteCommand( object sender, EventArgs e )
    {
        object parameter = PassArguments ? e : null;

        if ( Command != null )
        {
            if ( Command.CanExecute( parameter ) )
                Command.Execute( parameter );
        }
    }

    private Delegate handler;
    private EventInfo oldEvent;
}

请使用您xaml此行为按钮成参数传递RoutedEventArgs到您的命令是这样的:

<UserControl x:Class="YourNameSpace.YourClass">
xmlns:beh="clr-namespace:YourNameSpace.Behaviors"
...
<StackPanel Orientation="Horizontal" >
    <Button Margin="2" Content="Button 1" Name="btnButton1">
        <i:Interaction.Behaviors>
            <beh:EventToCommandBehavior Command="{Binding ButtonCommand}" Event="Click" PassArguments="True" />
        </i:Interaction.Behaviors>
    </Button>
    <Button Margin="2" Content="Button 2" Name="btnButton2">
        <i:Interaction.Behaviors>
            <beh:EventToCommandBehavior Command="{Binding ButtonCommand}" Event="Click" PassArguments="True" />
        </i:Interaction.Behaviors>
    </Button>

</StackPanel>

最后创建这样RelayCommand

public RelayCommand<RoutedEventArgs> ButtonCommand { get; }

ButtonCommand = new RelayCommand<RoutedEventArgs>( x => Foo(x) );

private void Foo( RoutedEventArgs args)
{
    string buttonName = ( ( System.Windows.FrameworkElement )args.Source ).Name;
    //Your switch statements..
}

同样,你还可以将更多的事件,如MouseEnterMouseLeave


0
投票

我认为,对于你这个工作

ViewModel.cs

 public class ViewModel
{
    public ICommand MouseEnterCommand
    {
        get
        {
            return new RelayCommand(a => this.Executemethod(a), p => Canexecutemethod(p));
        }
    }

    public bool Canexecutemethod(object a)
    {
        return true;
    }

    public void Executemethod(object p)
    {
        Console.WriteLine(DateTime.Now.ToLongTimeString());

        string name = Convert.ToString(p);

        switch (name)
        {
            default:
                break;
        }

    }
}

View.xaml ::

<UserControl
    xmlns:interact="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    ..>
    <Grid>

            <Button Height="25" Width="150">
                <interact:Interaction.Triggers>
                    <interact:EventTrigger EventName="MouseEnter">
                    <interact:InvokeCommandAction Command="{Binding  MouseEnterCommand}" CommandParameter="MouseEnter" />
                    </interact:EventTrigger>
                </interact:Interaction.Triggers>
            </Button>

    </Grid>
</UserControl>
© www.soinside.com 2019 - 2024. All rights reserved.