WPF Datagrid绑定不显示值。

时间:2022-08-25 14:18:17

I am total begginer with WPF and I try to make binding to datagrid in WPF

我完全是WPF的乞丐,我试图在WPF中与datagrid绑定

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.

我不知道为什么它不起作用。有线索吗?网格没有值。

4 个解决方案

#1


14  

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跟踪设置。在这里,您可以设置冗长的级别,并查看关于Ouptut窗口中数据绑定的重要信息。它节省了我的时间。

Now the reson. You declared MailCollection as public property of the Window but binding is made against DataContext by default.

现在的原因。您将MailCollection声明为窗口的公共属性,但默认情况下对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的一些特性,但仍然有用。

#2


1  

You have forgot write this in WindowDataGridTest() constructor.

您已经忘记在WindowDataGridTest()构造函数中编写这个。

this.DataContext = this;

#3


1  

You have not bound the ObservableCollection to the DataGrid.

您还没有将观测站绑定到DataGrid。

Here is the step to achieve your problem.

这是解决问题的步骤。

  1. Define a name for your DataGrid. (Let's say myDataGrid)

    为您的DataGrid定义一个名称。(假设myDataGrid)

  2. then insert the code below in the constructor of the code behind file

    然后将下面的代码插入到文件后面代码的构造函数中

    myDataGrid.DataContext = this.MailCollection;
    

And please look at this tutorial to learn more about data binding

请查看本教程以了解更多关于数据绑定的知识。

#4


0  

Same problem I faced then I solved it by incresing grid height. Make sure Your Grid height is enough to show data.

同样的问题,我通过增加网格高度来解决。确保你的网格高度足够显示数据。

#1


14  

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跟踪设置。在这里,您可以设置冗长的级别,并查看关于Ouptut窗口中数据绑定的重要信息。它节省了我的时间。

Now the reson. You declared MailCollection as public property of the Window but binding is made against DataContext by default.

现在的原因。您将MailCollection声明为窗口的公共属性,但默认情况下对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的一些特性,但仍然有用。

#2


1  

You have forgot write this in WindowDataGridTest() constructor.

您已经忘记在WindowDataGridTest()构造函数中编写这个。

this.DataContext = this;

#3


1  

You have not bound the ObservableCollection to the DataGrid.

您还没有将观测站绑定到DataGrid。

Here is the step to achieve your problem.

这是解决问题的步骤。

  1. Define a name for your DataGrid. (Let's say myDataGrid)

    为您的DataGrid定义一个名称。(假设myDataGrid)

  2. then insert the code below in the constructor of the code behind file

    然后将下面的代码插入到文件后面代码的构造函数中

    myDataGrid.DataContext = this.MailCollection;
    

And please look at this tutorial to learn more about data binding

请查看本教程以了解更多关于数据绑定的知识。

#4


0  

Same problem I faced then I solved it by incresing grid height. Make sure Your Grid height is enough to show data.

同样的问题,我通过增加网格高度来解决。确保你的网格高度足够显示数据。