2016-03-16 5 views
0

Я создал свойство attched, чтобы привязать MouseUpCommand и MouseDownCommand к моей ViewModel. Пока MouseDownCommand срабатывает, как и следовало ожидать, MouseUpCommand срабатывает спорадически. Кто-нибудь знает, что я делаю неправильно?MouseUpCommand срабатывает только спорадически

Вот мой XAML:

<ItemsControl ItemsSource="{Binding Page.Collection}" Grid.Row="1" Grid.Column="1" 
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <Canvas IsItemsHost="True" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
        Background="Blue" 
        local:MouseBehaviour.MouseUpCommand="{Binding ViewModel.MouseUpCommand}" 
        local:MouseBehaviour.MouseDownCommand="{Binding ViewModel.MouseDownCommand}" 
        local:MouseBehaviour.MouseMoveCommand="{Binding ViewModel.MouseMoveCommand}"> 
       </Canvas> 

Вот MouseBehavior:

public class MouseBehaviour 
{ 
    #region MouseUp 

    public static readonly DependencyProperty MouseUpCommandProperty = 
     DependencyProperty.RegisterAttached("MouseUpCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseUpCommandChanged))); 

    private static void MouseUpCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     FrameworkElement element = (FrameworkElement)d; 

     element.PreviewMouseUp += element_MouseUp; 
    } 

    static void element_MouseUp(object sender, MouseButtonEventArgs e) 
    { 
     FrameworkElement element = (FrameworkElement)sender; 

     ICommand command = GetMouseUpCommand(element); 

     command.Execute(e); 
    } 



    public static void SetMouseUpCommand(UIElement element, ICommand value) 
    { 
     element.SetValue(MouseUpCommandProperty, value); 
    } 

    public static ICommand GetMouseUpCommand(UIElement element) 
    { 
     return (ICommand)element.GetValue(MouseUpCommandProperty); 
    } 

    #endregion 

    #region MouseDown 

    public static readonly DependencyProperty MouseDownCommandProperty = 
     DependencyProperty.RegisterAttached("MouseDownCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseDownCommandChanged))); 

    private static void MouseDownCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     FrameworkElement element = (FrameworkElement)d; 

     element.PreviewMouseDown += element_MouseDown; 
    } 

    static void element_MouseDown(object sender, MouseButtonEventArgs e) 
    { 
     FrameworkElement element = (FrameworkElement)sender; 

     ICommand command = GetMouseDownCommand(element); 

     command.Execute(e); 
    } 

    public static void SetMouseDownCommand(UIElement element, ICommand value) 
    { 
     element.SetValue(MouseDownCommandProperty, value); 
    } 

    public static ICommand GetMouseDownCommand(UIElement element) 
    { 
     return (ICommand)element.GetValue(MouseDownCommandProperty); 
    } 

    #endregion 
} 

Я попытался с помощью обычных команд мыши, а также команды просмотра мыши. Нет разницы.

ответ

0

Если я нарушу шаблон MVVM и просто добавлять события в коде позади программы работает безотказно:

private void ItemsControl_MouseUp(object sender, MouseButtonEventArgs e) 
    { 
     mainViewModel.ViewModel.MouseUp(e); 
    } 

    private void ItemsControl_MouseMove(object sender, MouseEventArgs e) 
    { 
     mainViewModel.ViewModel.MouseMove(e); 
    } 

    private void ItemsControl_MouseDown(object sender, MouseButtonEventArgs e) 
    { 
     mainViewModel.ViewModel.MouseDown(e); 
    } 
Смежные вопросы