我创建了新的Uwp项目 我添加了MyToolkit Extended Dll
我在设计页面中添加了一个数据网格控件
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:MyControls="using:MyToolkit.Controls"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<MyControls:DataGrid Name="bala" VerticalAlignment="Stretch">
</MyControls:DataGrid>
</Grid>
</Page>
我添加的代码
//空白页面项目模板记录在https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
List<data> lst = new List<data>() { new data() { iD = "1", Name = "bala" }, new data() { iD = "1", Name = "bala" }, new data() { iD = "1", Name = "bala" } };
MyToolkit.Controls.DataGridTextColumn column = new MyToolkit.Controls.DataGridTextColumn();
column.Binding = new Binding() { ElementName="iD"};
column.Header = "ID";
bala.Columns.Add(column);
MyToolkit.Controls.DataGridTextColumn column1 = new MyToolkit.Controls.DataGridTextColumn();
column1.Binding = new Binding() { ElementName = "Name" };
column1.Header = "Name";
bala.Columns.Add(column1);
bala.ItemsSource = lst;
}
}
public class data
{
public string iD { get; set; }
public string Name { get; set; }
}
}
我面临崩溃问题。
但是设计方面我添加了它可以正常工作的列。代码方面我只面临问题。
<MyControls:DataGrid.Columns>
<MyControls:DataGridTextColumn Header="ID" Binding="{Binding iD}"></MyControls:DataGridTextColumn>
<MyControls:DataGridTextColumn Header="Name" Binding="{Binding Name}"></MyControls:DataGridTextColumn>
</MyControls:DataGrid.Columns>
我得到了运行时例外: 至少有一个对象必须实现IComparable。&#39;
答案 0 :(得分:1)
您应该使用路径进行绑定
column.Binding = new Binding() { Path = new PropertyPath("iD") };
column1.Binding = new Binding() { Path = new PropertyPath("Name") };
这样可以正常工作。