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

UWP点击单选按钮调用ReDraw后sfGrid为空 如何正确刷新

问题现象
  • 需求:点击RadioButton时根据传入的查询条件刷新sfGrid数据
  • 异常表现:sfGrid首次加载数据显示正常,在editablePage中调用ReDraw()方法后sfGrid显示为空
  • 已尝试无效方案:调用.View.Refresh()方法、通过onNavigateTo传参刷新
问题根因

ItemsCollection属性赋值时没有触发属性变更通知,sfGrid首次绑定可以拿到初始集合,但后续替换ItemsCollection为新的ObservableCollection实例时,UI层未收到绑定更新通知,无法感知数据源变化,最终显示为空。

修复步骤
  1. 让UserinfoModel实现INotifyPropertyChanged接口,为ItemsCollection属性添加属性变更通知,修改后代码结构如下:
public class UserinfoModel : INotifyPropertyChanged
{
    private ObservableCollection<FunctionDataGridModel> _itemsCollection;
    public ObservableCollection<FunctionDataGridModel> ItemsCollection
    {
        get => _itemsCollection;
        set
        {
            _itemsCollection = value;
            OnPropertyChanged(nameof(ItemsCollection));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    // 保留原有ReDraw、SetDataSource方法即可
    public void ReDraw(string rb_cond)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine(" SELECT A.USER_ID, A.USER_NM, A.USER_POS ,A.PERMISS ,A.CONST_DT ,A.MODIFY_DT ,A.REMARK ,C.SYS_NM ");
        sb.AppendLine(" FROM USER_INFO A ");
        sb.AppendLine(" LEFT OUTER JOIN CHARGE_PRJT B ON A.USER_ID = B.USER_ID ");
        sb.AppendLine(" LEFT OUTER JOIN SYS_INFO C ON C.SYS_ID = B.SYS_ID ");
        sb.AppendLine(" WHERE 1=1 ");

        if (rb_cond == "now")
        {
            sb.AppendLine(" AND A.USE_YN = 'Y' ");
        }
        else if (rb_cond == "del")
        {
            sb.AppendLine(" AND A.USE_YN = 'N' ");
        }

        _dt = AppInstance.Ins.dbHelper.GetDataTable(sb.ToString(), false);
        SetDataSource(_dt);
    }

    public void SetDataSource(DataTable dt)
    {
        if (ItemsCollection != null)
            ItemsCollection.Clear();

        ObservableCollection<FunctionDataGridModel> result = new ObservableCollection<FunctionDataGridModel>();
        foreach (System.Data.DataRow dr in dt.Rows)
        {
            FunctionDataGridModel rootModel = new FunctionDataGridModel();
            rootModel.id = dr["USER_ID"].ToString();
            rootModel.name = dr["USER_NM"].ToString();
            rootModel.group = dr["USER_POS"].ToString();
            rootModel.permiss = dr["PERMISS"].ToString();
            rootModel.project = dr["SYS_NM"].ToString();
            rootModel.startDT = dr["CONST_DT"].ToString();
            rootModel.endDt = dr["MODIFY_DT"].ToString();
            rootModel.remark = dr["REMARK"].ToString();

            rootModel.Item = rootModel;
            result.Add(rootModel);
        }
        this.ItemsCollection = result;
    }
}
  1. 优化RadioButton点击事件的空值判断,避免页面未加载完成时事件提前触发导致的异常:
private void NowUser_Checked(object sender, RoutedEventArgs e)
{
    if (DataGridFrame.Content is not Userinfo.userinfo01 page || page.DataContext is not UserinfoModel model)
        return;
    model.ReDraw("now");
}
  1. 检查XAML中sfGrid的绑定配置,确保ItemsSource绑定路径正确:
<syncfusion:SfGrid x:Name="sfGrid" ItemsSource="{Binding ItemsCollection}" />

如果完成以上配置后仍有显示异常,可以在ReDraw方法末尾追加sfGrid.View.Refresh()强制刷新视图即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.03 07:37:29