You need to enable JavaScript to run this app. Xamarin.Forms UWP DataGrid选中项如何在Entry控件中展示?
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin.Forms UWP DataGrid选中项如何在Entry控件中展示?

Xamarin.Forms UWP DataGrid 选中项同步至 Entry 实现

这个功能不需要单独编写UWP平台的特定逻辑,跨平台通用代码即可落地,两种常用实现方式可按需选择。

  • 前置准备:确保已通过NuGet安装对应DataGrid组件(社区版Xamarin.Forms.DataGrid、Syncfusion SfDataGrid 等均可,核心逻辑不依赖特定组件的私有API)
  • 实现效果匹配参考截图:选中表格任意行后,Entry自动展示对应选中行的拼接内容,无选中时自动清空文本。

快速实现版(适合首个项目快速验证效果)

直接利用控件自带的选中变化事件实现,无需搭建MVVM框架:

  1. 编写页面布局,注意XAML头部需引入DataGrid组件的命名空间,否则控件无法识别:
<ContentPage 
    xmlns:dg="clr-namespace:Xamarin.Forms.DataGrid;assembly=Xamarin.Forms.DataGrid">
    <VerticalStackLayout Padding="16" Spacing="16">
        <dg:DataGrid x:Name="SampleGrid"
                     ItemsSource="{Binding StudentList}"
                     SelectionChanged="OnGridSelectionChanged">
            <dg:DataGrid.Columns>
                <dg:DataGridColumn Title="学号" PropertyName="StuId"/>
                <dg:DataGridColumn Title="姓名" PropertyName="StuName"/>
                <dg:DataGridColumn Title="专业" PropertyName="Major"/>
            </dg:DataGrid.Columns>
        </dg:DataGrid>

        <Entry x:Name="SyncEntry" 
               Placeholder="选中表格行后内容会自动同步到这里"/>
    </VerticalStackLayout>
</ContentPage>
  1. 在页面的.xaml.cs后置代码中实现选中事件处理逻辑:
private void OnGridSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // 规避空引用,判断当前是否存在选中行
    if (SampleGrid.SelectedItem is Student selectedStu)
    {
        // 按需拼接需要展示的文本内容
        SyncEntry.Text = $"选中内容:学号 {selectedStu.StuId},姓名 {selectedStu.StuName},专业 {selectedStu.Major}";
    }
    else
    {
        // 无选中项时清空Entry内容
        SyncEntry.Text = string.Empty;
    }
}

注意:该事件在UWP平台的触发逻辑、选中项获取方式和其他平台完全一致,不需要单独做UWP平台的兼容处理。

MVVM 规范实现版(适合长期维护的项目)

通过双向绑定实现逻辑解耦,无需编写代码后置的事件逻辑:

  1. 在ViewModel中定义核心属性,完成选中项变化的通知逻辑:
public class SampleViewModel : INotifyPropertyChanged
{
    public ObservableCollection<Student> StudentList { get; } = new();

    // DataGrid 双向绑定的选中项属性
    private Student _selectedItem;
    public Student SelectedItem
    {
        get => _selectedItem;
        set
        {
            _selectedItem = value;
            OnPropertyChanged();
            // 选中项变化时主动通知Entry绑定的展示属性更新
            OnPropertyChanged(nameof(SyncedEntryText));
        }
    }

    // 为Entry提供单向绑定的同步展示文本
    public string SyncedEntryText => 
        SelectedItem == null 
            ? string.Empty 
            : $"选中内容:学号 {SelectedItem.StuId},姓名 {SelectedItem.StuName},专业 {SelectedItem.Major}";

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
  1. 调整页面控件的绑定配置,移除冗余事件:
<dg:DataGrid ItemsSource="{Binding StudentList}"
              SelectedItem="{Binding SelectedItem}"/>
<Entry Text="{Binding SyncedEntryText, Mode=OneWay}"/>

新手常见坑点提醒

  • 若出现选中后Entry不更新的问题,优先检查属性是否正确实现INotifyPropertyChanged变更通知,缺失通知会导致UI无法接收到属性变化。
  • 若需要支持多选模式,只需将DataGrid的SelectionMode设为Multiple,在选中集合变化时遍历所有选中项拼接展示文本即可,核心逻辑和单选完全一致。
  • 全程不需要为UWP平台编写自定义渲染、平台依赖服务这类平台特定代码,上述逻辑在UWP、Android、iOS平台可通用生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.07 10:45:05