c# - Failed binding data to DataGrid -



c# - Failed binding data to DataGrid -

can tell me, please , why cannot recieve desired result in column company (here, name of company). here have tried bind source info ( class person) wpf datagrid.

surname | name | company --------------------------------------- sidorov | sasha | datagridbind.company petrov | misha | datagridbind.company

mainwindow.xaml.cs:

namespace datagridbind { public partial class mainwindow : window { public mainwindow() { initializecomponent(); person person = new person("sasha", "sidorov", new company("teremok") ); person person1 = new person("misha", "petrov",new company("subway")); observablecollection<person> persons = new observablecollection<person> { person, person1 }; persondatagrid.itemssource = persons; } } }

person.cs :

namespace datagridbind { public class person { public string name { get; set; } public string surname { get; set; } public company company { get; set; } public person() { } public person(string _name, string _surname, company _company) { name = _name; surname = _surname; company = _company; } } public class company { public string name {get;set;} public company() { } public company(string _name) { name = _name; } } }

xaml:

<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:datagridbind" x:class="datagridbind.mainwindow" title="mainwindow" height="350" width="525"> <grid > <datagrid x:name="persondatagrid" autogeneratecolumns="false" enablerowvirtualization="true" margin="10,10,138,177" rowdetailsvisibilitymode="visible"> <datagrid.columns> <datagridtextcolumn x:name="namecolumn" binding="{binding name}" header="name" width="sizetoheader"/> <datagridtextcolumn x:name="surnamecolumn" binding="{binding surname}" header="surname" width="sizetoheader"/> <datagridtextcolumn x:name="companycolumn" binding="{binding company}" header="company" width="200"/> </datagrid.columns> </datagrid> </grid> </window>

because company class need specify property of company want bind to

<datagridtextcolumn binding="{binding company.name}" header="company" ... />

otherwise tostring() called on bound object

c# wpf

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -