【问题标题】:Firing scrollchanged event in wpf application在 wpf 应用程序中触发 scrollchanged 事件
【发布时间】:2016-01-30 15:25:42
【问题描述】:

我有这个代码

查看

    <DataGrid x:Name="dgFactures" ScrollViewer.VerticalScrollBarVisibility="Auto"  Width="auto">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="ScrollChanged">
                                    <cmd:EventToCommand Command="{Binding ScrollChangedCommand}" PassEventArgsToCommand="True"></cmd:EventToCommand>
                                </i:EventTrigger>
                             </i:Interaction.Triggers>
              <DataGrid.Columns>
                        <DataGridTemplateColumn Header="Hébergement">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <CheckBox IsChecked="{Binding Hebergement,  Converter={StaticResource NullToFalse}}">
                                                <i:Interaction.Triggers>
                                                    <i:EventTrigger EventName="Checked">
                                                        <cmd:EventToCommand Command="{Binding HebergementCommand}" PassEventArgsToCommand="True"></cmd:EventToCommand>
                                                    </i:EventTrigger>
                                                    <i:EventTrigger EventName="Unchecked">
                                                        <cmd:EventToCommand Command="{Binding HebergementCommand}" PassEventArgsToCommand="True"></cmd:EventToCommand>
                                                    </i:EventTrigger>
                                                </i:Interaction.Triggers>

                                            </CheckBox>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
          </DataGrid.Columns>
</DataGrid>

视图模型

 public RelayCommand<RoutedEventArgs> HebergementCommand { get; set; }
 public RelayCommand<ScrollChangedEventArgs> ScrollChangedCommand { get; set; }
  HebergementCommand = new RelayCommand<RoutedEventArgs>((e) =>
            {
                PropertyInfo IsChekedInfo = e.Source.GetType().GetProperty("IsChecked");
                bool isChecked = (bool)IsChekedInfo.GetValue(e.Source, null);
                Hebergement = isChecked;
            });

            ScrollChangedCommand = new RelayCommand<ScrollChangedEventArgs>((e) =>
            {
                if (e.HorizontalChange != 0)
                {
                    // Do stuff..
                }
            });

问题是:

  • 当我使用鼠标或单击滚动条在网格中滚动时:Checked 事件被触发,ScrollChanged 事件没有触发。
  • ScrollChanged 根本没有被解雇

我需要知道:

  1. 为什么会这样?
  2. 我该如何解决?

谢谢

【问题讨论】:

    标签: c# .net wpf xaml mvvm


    【解决方案1】:

    您的问题是由EventTriggerBase 的工作方式引起的。如果你看一下它的代码,你会看到:

    private void RegisterEvent(object obj, string eventName)
    {
        Type type = obj.GetType();
        EventInfo @event = type.GetEvent(eventName);
        if (@event == null)
        {
            if (this.SourceObject != null)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ExceptionStringTable.EventTriggerCannotFindEventNameExceptionMessage, new object[]
                {
                    eventName,
                    obj.GetType().Name
                }));
            }
            return;
        }
        /* and so on... */
    }
    

    但重点是它使用反射来查找将被触发的事件。因此它找不到附加到 FrameworkElement(在您的情况下为 DataGrid)的 attached events

    为解决此问题,请编写您自己的 EventTrigger:

    public class AttachedEventTrigger : EventTriggerBase<DependencyObject>
    {
        public RoutedEvent RoutedEvent { get; set; }
    
        private FrameworkElement AssociatedElement
        {
            get
            {
                IAttachedObject attachedObject = AssociatedObject as IAttachedObject;
                FrameworkElement associatedElement = AssociatedObject as FrameworkElement;
    
                if (attachedObject != null)
                {
                    associatedElement = attachedObject.AssociatedObject as FrameworkElement;
                }
    
                return associatedElement;
            }
        }
    
        protected override void OnAttached()
        {
            FrameworkElement associatedElement = AssociatedElement;
    
            if (associatedElement == null)
            {
                throw new InvalidOperationException();
            }
    
            if (RoutedEvent != null)
            {
                associatedElement.AddHandler(RoutedEvent, new RoutedEventHandler(OnRoutedEvent));
            }
        }
    
        protected override void OnDetaching()
        {
            FrameworkElement associatedElement = AssociatedElement;
    
            if (RoutedEvent != null)
            {
                associatedElement.RemoveHandler(RoutedEvent, new RoutedEventHandler(OnRoutedEvent));
            }
        }
    
        private void OnRoutedEvent(object sender, RoutedEventArgs args)
        {
            OnEvent(args);
        }
    
        protected override string GetEventName()
        {
            if (RoutedEvent != null)
            {
                return RoutedEvent.Name;
            }
    
            return String.Empty;
        }
    }
    

    你可以这样使用:

    <DataGrid x:Name="dgFactures" ScrollViewer.VerticalScrollBarVisibility="Auto" Width="auto">
        <i:Interaction.Triggers>
            <local:AttachedEventTrigger RoutedEvent="ScrollViewer.ScrollChanged">
                <!-- your actions -->
            </local:AttachedEventTrigger>
        </i:Interaction.Triggers>
        <DataGrid.Columns>
            <!-- your columns -->
        </DataGrid.Columns>
    </DataGrid>
    

    我希望这个示例可以帮助到你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2020-11-03
      • 2011-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多