【发布时间】:2020-11-25 16:08:59
【问题描述】:
我正在尝试用一些数据填充 GridView,但没有运气。文档完全没有告诉我 GridView 如何将数据从项目源映射到控件中的行。我已经看到了他们创建具有属性的类的文本框示例,并且效果很好:
public sealed partial class CollectionInfoPage: Page
{
List<Test> m_ItemSource;
class Test
{
public string s1 => "Hello";
public string s2 => "World";
}
public async Task ModelUpdated(Model.Adaptor Adaptor)
{
await Common.RunUiTask(async () =>
{
m_ItemSource = new List<Test> { new Test(), new Test() };
});
}
}
XAML:
<Page
x:Class="CollectionInfoPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="Transparent">
<Grid>
<controls:DataGrid x:Name="DataGrid" AutoGenerateColumns="True" ItemsSource="{x:Bind m_ItemSource}">
</controls:DataGrid>
</Grid>
</Page>
但我的问题是,列名在编译时根本不知道,所以我不能使用这种方法。我需要能够在运行时指定列的名称并将数据映射到那些特定的列,但这就是我卡住的地方。我想不出办法。我找不到任何关于它如何将数据从数据集映射到不同列的文档。我几乎可以猜到,但这对我没有好处。
我发现整个数据绑定方法很糟糕、不灵活、缓慢且难以使用。为什么不能简单地使用接口来进行数据绑定而不是获取对象?
关于如何实现这一点的任何线索?
解决方案: 我定义了一个 DataView 类用作项源,如下所示:
class DataView
{
public IEnumerable<string> Data { get; }
public DataView(IEnumerable<string> Data)
{
this.Data = Data;
}
}
然后我使用代码创建列并创建与项目源中数据的绑定:
int n = 0;
var Cols = ColNames.Select((x, n) =>
{
var Col = new DataGridTextColumn();
Col.Header = x;
Col.Binding = new Windows.UI.Xaml.Data.Binding();
Col.Binding.Path = new PropertyPath("Data[" + n + "]");
return Col;
}).ToList();
foreach (var Col in Cols)
DataGrid.Columns.Add(Col);
XAML:
<Page
x:Class="CollectionInfoPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="Transparent">
<Grid>
<controls:DataGrid x:Name="DataGrid" AutoGenerateColumns="False" ItemsSource="{x:Bind m_GridItemSource}"/>
</Grid>
</Page>
然后我只需要设置我的项目源,其中每个 DataView 实例将包含一行信息。
【问题讨论】:
标签: c# uwp datagrid windows-community-toolkit