WPF Datagrid binding does not show values

拜拜、爱过 提交于 2019-12-08 06:19:34

问题


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

here is XAML code

<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

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.


回答1:


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.

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

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.




回答2:


You have forgot write this in WindowDataGridTest() constructor.

this.DataContext = this;



回答3:


You have not bound the ObservableCollection to the DataGrid.

Here is the step to achieve your problem.

  1. Define a name for your DataGrid. (Let's say 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:


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



来源:https://stackoverflow.com/questions/15761124/wpf-datagrid-binding-does-not-show-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!