WPF MVVM 模式如何监听IsVisibleChanged 事件

原本以为这是一个很简单的问题,但是我却走了不少的弯路。记录下来自省。

我使用的是库System.Windows.Interactivity.dll,首先在xaml 中使用了EventTrrigger

复制代码
<!--  当 IsVisibleChanged 事件触发时,执行绑定的命令  -->
<!--
            <i:EventTrigger EventName="IsVisibleChanged">
                <i:InvokeCommandAction Command="{Binding ElementName=MainGrid, Path=DataContext.IsVisibleChangedCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>

但是发现是无效的。经过 改变IvokeCommandAction的参数,等方式,发现都无法触发。

最后决定使用behavior 来进行

接下来的写法如下

1、首先定义一个Behavior,

(注意:这里我也遇到了一个问题,原本我binding 的是Visibility 这个属性的,但是我也发现无法触发。通过测试发现,不是代码的问题。而是Visibility 从始至终都是Visibility.Visible ,但是IsVisible的属性,确实变化过的。这有点超乎我的认知。我认为这两个应该是同步的。所以最终attach 到IsVisible 的属性上)

复制代码
    public class VisibilityMonitorBehavior : Behavior<UserControl>
    {
        // 定义一个 DependencyProperty 用于绑定 Visibility 属性
        public static readonly DependencyProperty VisibilityProperty =
            DependencyProperty.Register(
                "IsVisible",
                typeof(bool),
                typeof(VisibilityMonitorBehavior),
                new PropertyMetadata(true, OnVisibilityChanged));

        private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is VisibilityMonitorBehavior behavior && behavior.AssociatedObject != null)
            {
                var newVisibility = (bool)e.NewValue;
                if (newVisibility == true)
                {
                    (behavior.AssociatedObject.DataContext as UIViewModelBase).EnableTimer(true);
                }
                else
                {
                    (behavior.AssociatedObject.DataContext as UIViewModelBase).EnableTimer(false);
                }
                LOG.Info($"{behavior.AssociatedObject.Name}Visibility changed to: {newVisibility}");
            }
        }

        public bool IsVisible
        {
            get => (bool)GetValue(VisibilityProperty);
            set => SetValue(VisibilityProperty, value);
        }
        // 关联控件时初始化绑定
        protected override void OnAttached()
        {
            base.OnAttached();

            if (AssociatedObject != null)
            {
                // 绑定 AssociatedObject 的 Visibility 属性到行为的 VisibilityProperty
                var binding = new System.Windows.Data.Binding("IsVisible")
                {
                    Source = AssociatedObject,
                    Mode = System.Windows.Data.BindingMode.OneWay
                };
                BindingOperations.SetBinding(this, VisibilityProperty, binding);
            }
        }

        // 移除控件时清理资源
        protected override void OnDetaching()
        {
            BindingOperations.ClearBinding(this, VisibilityProperty);
            base.OnDetaching();
        }

    }

通过这种的方式,终于可以监听到IsVisibleChanged 事件了。

值得记录的是,我需要动态添加这个behavior ,因此把这部分代码也贴出来。以供后期查看。

其中control就是控件

复制代码
// 动态 添加 VisibilityMonitorBehavior
            var behaviors = Interaction.GetBehaviors(control);
            var visibilityBehavior = new VisibilityMonitorBehavior();
            behaviors.Add(visibilityBehavior);
相关推荐
明耀13 小时前
WPF DataGrid 默认显示行号
wpf
lph197217 小时前
wpf的converter
wpf
fyifei055817 小时前
WPF学习PropertyChanged
wpf
爱炸薯条的小朋友17 小时前
C#由于获取WPF窗口名称造成的异常报错问题
windows·c#·wpf
baivfhpwxf202318 小时前
wpf ListBox 去除item 单击样式
wpf
诗仙&李白18 小时前
lnnovationHubTool,用prism+WPF编写的MVVM模式的快速上位机软件开发框架平台
wpf·mvvm·prism·上位机软件开发框架平台
程序员小刘1 天前
【HarmonyOS 5】教育开发实践详解以及详细代码案例
华为·wpf·harmonyos
Java Fans1 天前
在WPF项目中集成Python:Python.NET深度实战指南
python·.net·wpf
布伦鸽2 天前
C# WPF 左右布局实现学习笔记(1)
笔记·学习·c#·wpf
code bean3 天前
【WPF】WPF 项目实战:构建一个可增删、排序的光源类型管理界面(含源码)
wpf