WPF Datagrid 绑定不显示值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15761124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Datagrid binding does not show values
提问by Robert
I am total begginer with WPF and I try to make binding to datagrid in WPF
我完全是 WPF 的初学者,我尝试在 WPF 中绑定到数据网格
here is XAML code
这是 XAML 代码
<Grid x:Name="LayoutRoot">
<Grid HorizontalAlignment="Left" Height="440" VerticalAlignment="Top" Width="632">
<DataGrid HorizontalAlignment="Left" Height="420" Margin="10,10,0,0" VerticalAlignment="Top" Width="603" ItemsSource="{Binding Source=MailCollection}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="id" Binding="{Binding Id}"/>
<DataGridTextColumn Header="nazwa" Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Grid>
Here is MailTpl class
这是 MailTpl 类
public class MailTpl
{
public string Id { get; set; }
public string Name { get; set; }
}
And here is how I do binding
这是我如何绑定
public partial class WindowDataGridTest : Window
{
ObservableCollection<MailTpl> _MailCollection = new ObservableCollection<MailTpl>();
public ObservableCollection<MailTpl> MailCollection { get { return _MailCollection; } }
public WindowDataGridTest()
{
_MailCollection.Add(new MailTpl { Id= "abbb", Name = "badfasdf" });
_MailCollection.Add(new MailTpl { Id = "asasdfasdfdf", Name = "basdfasdfaa" });
this.InitializeComponent();
// Insert code required on object creation below this point.
}
}
I don't know why it does not work. Any clues? Grid shows no values.
我不知道为什么它不起作用。有什么线索吗?网格不显示值。
回答by Pavel Voronin
Just an advice for the future.
只是对未来的建议。
Visual studio -> Options -> Debugging -> Output Window -> WPF Trace Settings. Here you can set the level of verbosity and see important information about data binding in Ouptut window. It saved me hours.
Visual Studio -> 选项 -> 调试 -> 输出窗口 -> WPF 跟踪设置。您可以在此处设置详细程度并查看有关输出窗口中数据绑定的重要信息。它节省了我几个小时。
Now the reson. You declared MailCollection as public property of the Window but binding is made against DataContext by default.
现在的共鸣。您将 MailCollection 声明为 Window 的公共属性,但默认情况下是针对 DataContext 进行绑定的。
So, you have two ways:
所以,你有两种方法:
this.DataContext = _MailCollection
and change binding a little to
并稍微改变绑定
ItemsSource={Binding}
or just change binding to this:
或者只是改变绑定到这个:
ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=MailCollection}"
I also recommend this pdf binding cheat sheet. It lacks some WPF 4.5 features but still useful.
我还推荐这个pdf 装订备忘单。它缺少一些 WPF 4.5 功能,但仍然有用。
回答by Farhad Jabiyev
You have forgot write this in WindowDataGridTest() constructor.
你忘了在 WindowDataGridTest() 构造函数中写这个。
this.DataContext = this;
回答by Haritha
You have notbound the ObservableCollection to the DataGrid.
您尚未将 ObservableCollection 绑定到 DataGrid。
Here is the step to achieve your problem.
这是解决您的问题的步骤。
Define a name for your DataGrid. (Let's say
myDataGrid)then insert the code below in the constructor of the code behind file
myDataGrid.DataContext = this.MailCollection;
为您的 DataGrid 定义一个名称。(让我们说
myDataGrid)然后在代码隐藏文件的构造函数中插入下面的代码
myDataGrid.DataContext = this.MailCollection;
And please look at this tutorialto learn more about data binding
请查看本教程以了解有关数据绑定的更多信息
回答by TAHA SULTAN TEMURI
Same problem I faced then I solved it by incresing grid height. Make sure Your Grid height is enough to show data.
我遇到了同样的问题,然后我通过增加网格高度来解决它。确保您的网格高度足以显示数据。

