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);
相关推荐
code_shenbing8 小时前
WPF实现打印机控制及打印
wpf
界面开发小八哥1 天前
界面组件DevExpress WPF中文教程:Grid - 如何显示和隐藏列?
wpf·界面控件·devexpress·ui开发·.net9
虚假程序设计1 天前
python用 PythonNet 从 Python 调用 WPF 类库 UI 用XAML
python·ui·wpf
落落落sss1 天前
MongoDB
数据库·windows·redis·mongodb·微服务·wpf
蒋劲豪1 天前
WPF项目暴露WebApi接口;WinForm项目暴露WebApi接口;C#项目暴露WebApi接口;
开发语言·c#·wpf
狮歌~资深攻城狮2 天前
未来已来:HBase的新功能与发展趋势展望
大数据·wpf·hbase
界面开发小八哥3 天前
界面控件DevExpress WPF v24.2新版亮点:支持.NET 9
.net·wpf·界面控件·devexpress·ui开发·用户界面
九鼎科技-Leo4 天前
WPF快速创建DeepSeek本地自己的客户端-基础思路版本
wpf
MasterNeverDown5 天前
WPF 中为 Grid 设置背景图片全解析
大数据·hadoop·wpf
苏克贝塔5 天前
WPF8-常用控件
wpf