WPF 中的数据网格绑定
我知道已经有人问过这个问题,但我已经完成了开发人员建议的几乎所有事情。
<DataGrid x:Name="Imported" VerticalAlignment="Top"
DataContext="{Binding Source=list}"
AutoGenerateColumns="False" CanUserResizeColumns="True">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Path=ID}"/>
<DataGridTextColumn Header="Date" Binding="{Binding Path=Date}"/>
</DataGrid.Columns>
</DataGrid>
我试图在模式对话框中显示这一点,并在模式对话框的构造函数中填充许可证列表。 但 DataGrid 内仍然没有填充任何内容。
构造函数代码:
public diagboxclass()
{
List<object> list = new List<object>();
list = GetObjectList();
}
public class object
{
string id;
DateTime date;
public string ID
{
get { return id; }
set { id = value; }
}
public DateTime Date
{
get { return date; }
set { date = value; }
}
}
你们认为与对象列表有关吗?
I know this has been asked already but I have done almost everything what is suggested by developers.
<DataGrid x:Name="Imported" VerticalAlignment="Top"
DataContext="{Binding Source=list}"
AutoGenerateColumns="False" CanUserResizeColumns="True">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Path=ID}"/>
<DataGridTextColumn Header="Date" Binding="{Binding Path=Date}"/>
</DataGrid.Columns>
</DataGrid>
I am trying to show this in modal dialog box and populating the license list in the constructor of the modal dialog box.
But still nothing is getting populated inside the DataGrid
.
Constructor code:
public diagboxclass()
{
List<object> list = new List<object>();
list = GetObjectList();
}
public class object
{
string id;
DateTime date;
public string ID
{
get { return id; }
set { id = value; }
}
public DateTime Date
{
get { return date; }
set { date = value; }
}
}
Do you guys think something to do with the object list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请不要使用object作为类名:
您应该为此类实现
INotifyPropertyChanged
,当然在属性设置器上调用它。否则更改不会反映在您的用户界面中。您的 Viewmodel 类/对话框类应该具有
MyObject
列表的Property
。ObservableCollection
是正确的方法:在
xaml
中,您应该将Itemssource
设置为MyObject
集合>。 (Datacontext
必须是您的对话框类!)PLEASE do not use object as a class name:
You should implement
INotifyPropertyChanged
for this class and of course call it on the Property setter. Otherwise changes are not reflected in your ui.Your Viewmodel class/ dialogbox class should have a
Property
of yourMyObject
list.ObservableCollection<MyObject>
is the way to go:In your
xaml
you should set theItemssource
to your collection ofMyObject
. (theDatacontext
have to be your dialogbox class!)如果没有看到所述对象列表,我相信您应该绑定到
DataGrid
的ItemsSource
属性,而不是它的DataContext
。(这假设包含
DataGrid
的元素 [UserControl
等] 的其DataContext
绑定到包含list
集合的对象DataGrid
派生自ItemsControl
,它依赖于其ItemsSource
属性来定义。其行绑定到的集合因此,如果。list
不是绑定到控件的DataContext
的对象的属性,您可能需要同时设置DataContext={Binding list}
和 <DataGrid
上的 code>ItemsSource={Binding list})。Without seeing said object list, I believe you should be binding to the
DataGrid
'sItemsSource
property, not itsDataContext
.(This assumes that the element [
UserControl
, etc.] that contains theDataGrid
has itsDataContext
bound to an object that contains thelist
collection. TheDataGrid
is derived fromItemsControl
, which relies on itsItemsSource
property to define the collection it binds its rows to. Hence, iflist
isn't a property of an object bound to your control'sDataContext
, you might need to set bothDataContext={Binding list}
andItemsSource={Binding list}
on theDataGrid
).尝试在后面的代码中执行此操作:
还要确保您的列表已有效填充,并且正如 @Blindmeis 提到的,切勿使用已在 C# 中给出函数的单词。
Try to do this in the behind code:
Also be sure your list is effectively populated and as mentioned by @Blindmeis, never use words that already are given a function in C#.