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

WinUI3 DataGrid实现按钮条件可见及类似Row_Update功能的方法

WinUI3 DataGrid中根据Status控制按钮可见性的解决方案

下面提供几种适配WinUI3的实现方案,可根据你的场景选择:

方法1:数据绑定+值转换器(推荐MVVM场景)

这种方式能自动响应Status的变化,符合WPF/WinUI的绑定思想。

首先创建一个值转换器,将Status值转换为Visibility枚举:

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;

public class StatusToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value is string status)
        {
            // 这里替换成你的业务逻辑,比如仅当Status为"待发送"时显示按钮
            return status == "待发送" ? Visibility.Visible : Visibility.Collapsed;
        }
        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

在XAML页面的资源中声明转换器:

<Page.Resources>
    <local:StatusToVisibilityConverter x:Key="StatusToVisibilityConverter" />
</Page.Resources>

修改按钮的Visibility绑定:

<controls:DataGridTemplateColumn Header="Action">
    <controls:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Click="SendAsync" 
                    CommandParameter="{Binding MessageId}"
                    Visibility="{Binding Status, Converter={StaticResource StatusToVisibilityConverter}}">Send</Button>
        </DataTemplate>
    </controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>

方法2:x:Bind条件表达式(适合简单场景)

如果你的数据模型实现了INotifyPropertyChanged,可以直接在XAML中写条件逻辑:

<controls:DataGridTemplateColumn Header="Action">
    <controls:DataGridTemplateColumn.CellTemplate>
        <DataTemplate x:DataType="local:YourDataModel"> <!-- 替换为你的数据模型类型 -->
            <Button Click="SendAsync" 
                    CommandParameter="{Binding MessageId}"
                    Visibility="{x:Bind Status == '待发送' ? Visibility.Visible : Visibility.Collapsed, Mode=OneWay}">Send</Button>
        </DataTemplate>
    </controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>

方法3:后台代码处理(类似WinForm的RowUpdate思路)

如果你习惯在后台代码控制,可监听DataGrid的LoadingRow事件:

先给按钮命名:

<Button x:Name="SendBtn" Click="SendAsync" CommandParameter="{Binding MessageId}">Send</Button>

注册并实现事件逻辑:

public MainPage()
{
    this.InitializeComponent();
    YourDataGrid.LoadingRow += YourDataGrid_LoadingRow; // 替换为你的DataGrid名称
}

private void YourDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
    var dataItem = e.Row.DataContext as YourDataModel; // 替换为你的数据模型类型
    if (dataItem != null)
    {
        var sendBtn = e.Row.FindName("SendBtn") as Button;
        if (sendBtn != null)
        {
            sendBtn.Visibility = dataItem.Status == "待发送" ? Visibility.Visible : Visibility.Collapsed;
        }
    }
}

注意:这种方式无法自动响应Status的实时变化,需手动触发行重新加载。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 13:03:27