You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF中如何在DataGridRow样式里绑定IsVisibleChanged事件处理程序?

能否在DataGridRow的样式中为IsVisibleChanged事件附加处理程序?

你尝试的写法无法生效:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <EventSetter Event="IsVisibleChanged" Handler="OnIsVisibleChanged"/>
    </Style>
</DataGrid.RowStyle>

原因说明

EventSetter仅支持路由事件(RoutedEvent),而IsVisibleChanged是常规CLR事件,不属于路由事件范畴,因此无法通过EventSetter绑定处理程序。

可行替代方案

方案1:使用Blend行为绑定CLR事件

通过Microsoft.Xaml.Behaviors.Wpf(需安装对应NuGet包)的行为机制绑定事件:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="behaviors:Interaction.Behaviors">
            <Setter.Value>
                <behaviors:BehaviorCollection>
                    <local:RowVisibilityBehavior/>
                </behaviors:BehaviorCollection>
            </Setter.Value>
        </Setter>
    </Style>
</DataGrid.RowStyle>

对应的行为类实现:

using Microsoft.Xaml.Behaviors;
using System.Windows.Controls;
using System.Windows;

public class RowVisibilityBehavior : Behavior<DataGridRow>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.IsVisibleChanged += OnRowIsVisibleChanged;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.IsVisibleChanged -= OnRowIsVisibleChanged;
    }

    private void OnRowIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        var row = sender as DataGridRow;
        bool isVisible = (bool)e.NewValue;
        // 编写你的业务逻辑,比如记录行可见状态
    }
}

方案2:自定义附加属性

创建附加属性来绑定IsVisibleChanged事件处理程序:

using System.Windows;
using System.Windows.Controls;

public static class RowVisibilityHelper
{
    public static readonly DependencyProperty VisibilityChangedHandlerProperty =
        DependencyProperty.RegisterAttached(
            "VisibilityChangedHandler",
            typeof(DependencyPropertyChangedEventHandler),
            typeof(RowVisibilityHelper),
            new PropertyMetadata(OnVisibilityChangedHandlerChanged));

    public static void SetVisibilityChangedHandler(DependencyObject obj, DependencyPropertyChangedEventHandler value)
    {
        obj.SetValue(VisibilityChangedHandlerProperty, value);
    }

    public static DependencyPropertyChangedEventHandler GetVisibilityChangedHandler(DependencyObject obj)
    {
        return (DependencyPropertyChangedEventHandler)obj.GetValue(VisibilityChangedHandlerProperty);
    }

    private static void OnVisibilityChangedHandlerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is DataGridRow row)
        {
            if (e.OldValue is DependencyPropertyChangedEventHandler oldHandler)
            {
                row.IsVisibleChanged -= oldHandler;
            }
            if (e.NewValue is DependencyPropertyChangedEventHandler newHandler)
            {
                row.IsVisibleChanged += newHandler;
            }
        }
    }
}

在XAML中使用该附加属性:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="local:RowVisibilityHelper.VisibilityChangedHandler" Value="OnIsVisibleChanged"/>
    </Style>
</DataGrid.RowStyle>

后台实现处理方法:

private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    // 你的业务逻辑代码
}

内容的提问来源于stack exchange,提问作者Marcin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 12:31:00