WPF DataGrid绑定到string.Length而不是字符串文本
发布时间:2020-12-17 00:07:37 所属栏目:大数据 来源:网络整理
导读:我是 WPF的新手,我真的想尽可能地弄清楚自己… 我已经创建了我的第一个DataGrid控件,我试图用一个字符串列表填充它,如下所示: UserControl x:Class="DataGridTest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://sch
我是
WPF的新手,我真的想尽可能地弄清楚自己…
我已经创建了我的第一个DataGrid控件,我试图用一个字符串列表填充它,如下所示: <UserControl x:Class="DataGridTest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid Style="{StaticResource ContentRoot}"> <DataGrid Name="MyDataGrid" ItemsSource="{Binding}" HorizontalAlignment="Stretch" Height="500"/> </Grid> </UserControl> Private Sub DataGridTest_Loaded(sender As Object,e As RoutedEventArgs) Handles Me.Loaded Dim StringCollection As List(Of String) = New List(Of String) StringCollection.Add("Test") StringCollection.Add("Test1") StringCollection.Add("Test2") StringCollection.Add("Test3") StringCollection.Add("Test4") MyDataGrid.DataContext = StringCollection End Sub 但是,它实际上填充了标题“长度”,值为字符串的长度.为什么它本身没有填充字符串值? 很明显,我在这里缺少一些基本的东西,但我不能为我的生活弄清楚它是什么. 提前致谢!
因为DataGrid默认情况下会自动生成给定项类的列,所以它将搜索String类中可以转换为列的属性.您可以通过创建自己的列并关闭AutoGenerateColumns来避免这种情况
<DataGrid Name="MyDataGrid" ItemsSource="{Binding}" ... AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding}" Header="Something"/> </DataGrid.Columns> </DataGrid> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |