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

UWP中如何用VisualState.StateTriggers折叠IsDeleted为true的DataGrid行

解决UWP DataGrid通过VisualState.StateTriggers控制行可见性/禁用的问题

现有代码的问题

  1. 属性绑定错误:数据源属性是IsDeleted,但XAML中绑定了不存在的Delete属性
  2. XAML语法错误:重复闭合了<VisualState.StateTriggers>标签
  3. 转换器冗余:IsDeleted本身是布尔类型,无需VisibilityToBoolConverter,直接绑定即可

修正后的实现步骤

1. 完善数据源类(实现属性变更通知)

将字段改为属性,并实现INotifyPropertyChanged,确保属性值变化时UI能同步更新:

using System.ComponentModel;
using System.Runtime.CompilerServices;

public class StudentsList : INotifyPropertyChanged
{
    private int _id;
    private string _name;
    private bool _isDeleted;

    public int Id
    {
        get => _id;
        set
        {
            _id = value;
            OnPropertyChanged();
        }
    }

    public string Name
    {
        get => _name;
        set
        {
            _name = value;
            OnPropertyChanged();
        }
    }

    public bool IsDeleted
    {
        get => _isDeleted;
        set
        {
            _isDeleted = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

2. 修正DataGrid行样式的XAML

修复语法错误,正确绑定IsDeleted属性,通过VisualState触发器控制行的可见性或禁用状态:

<Style x:Key="RowStyle" TargetType="controls:DataGridRow">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="RowVisibilityStates">
            <!-- 当IsDeleted为true时,折叠行 -->
            <VisualState>
                <VisualState.StateTriggers>
                    <StateTrigger IsActive="{Binding IsDeleted}" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Property="Visibility" Value="Collapsed" />
                    <!-- 如果需要禁用行而不是隐藏,替换为下面的Setter -->
                    <!-- <Setter Property="IsEnabled" Value="False" /> -->
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
</Style>

<controls:DataGrid
    x:Name="grid"
    AutoGenerateColumns="False"
    VerticalAlignment="Stretch"
    HorizontalAlignment="Stretch"
    RowStyle="{StaticResource RowStyle}">
    <!-- 自定义列定义 -->
    <controls:DataGrid.Columns>
        <controls:DataGridTextColumn Header="ID" Binding="{Binding Id}" />
        <controls:DataGridTextColumn Header="姓名" Binding="{Binding Name}" />
    </controls:DataGrid.Columns>
</controls:DataGrid>

补充说明

  • 当IsDeleted属性为true时,StateTrigger会激活对应的VisualState,自动将行设为隐藏或禁用状态
  • 确保DataGrid已正确绑定数据源,示例代码:
grid.ItemsSource = new ObservableCollection<StudentsList>
{
    new StudentsList { Id = 1, Name = "张三", IsDeleted = false },
    new StudentsList { Id = 2, Name = "李四", IsDeleted = true } // 这行会被隐藏/禁用
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 04:15:37