Sunday 15 February 2015

Binding textbox from Datagrid (WPF) -



Binding textbox from Datagrid (WPF) -

i having problem xaml. want give textbox binding datagridview. i've written far:

<textbox x:name="txtmamh" horizontalalignment="left" height="23" margin="178,78,0,0" textwrapping="wrap" text="{binding selecteditem.mamh, elementname=dgvmh}" verticalalignment="top" width="120"/>

but received 2 errors:

binding not supported in windows presentation foundation (wpf) project.

the type 'binding' not found. verify not missing assembly reference , referenced assemblies have been built.

how prepare this? please help me, give thanks you

and 1 more thing. have datagridview (dgvmh) has 2 columns mamon, tenmon. want fill textboxes txtmamon, txttenmon whenever click on row in dgvmh. true xaml code? right me if i'm wrong, thanks

<textbox x:name = "txtmamon" text="{binding selecteditem.mamon, elementname=dgvmh}"/> <textbox x:name = "txttenmon" text="{binding selecteditem.tenmon, elementname=dgvmh}"/> <datagrid x:name="dgvmh" />

two issues can see right off bat.

when says "binding" not supporting in wpf project, it's referring word used in xaml seek bind text property. typo - should read {binding ... }, not {binding ... }

secondly, regarding mamon , tenmon, selecteditem (presumably datagridcell or of - that's typo when "datagridview," right?) doesn't have properties based on columns, , if did, selected cell isn't going know column other 1 it's in. can't access columns adding .columnname after item. (it's possible i'm misunderstanding here - variable names don't communicate much me.)

if you're asking how fill textbox currently-selected items, should bind selecteditem property. here's illustration of how to little test viewmodel:

here's datagrid xaml:

<datagrid name="datagrid1" itemssource="{binding items}" selecteditem="{binding selectedcells, mode=onewaytosource}" />

and here's testviewmodel , testmodel:

public class testviewmodel : inotifypropertychanged { public testviewmodel() { this.items = new list<testmodel>() { new testmodel { companyname = "a", firstname = "b", lastname = "c" }, new testmodel { companyname = "1", firstname = "2", lastname = "3" } }; } public list<testmodel> items { get; set; } private testmodel selectedcells; public testmodel selectedcells { { homecoming selectedcells; } set { selectedcells = value; notifypropertychanged("selectedcells"); } } public event propertychangedeventhandler propertychanged; private void notifypropertychanged(string propertyname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } } public class testmodel { public string companyname { get; set; } public string firstname { get; set; } public string lastname { get; set; } }

the testviewmodel's items property provides source items within datagrid. when 1 of these selected, alter in datagrid's selecteditem property pushed property in testviewmodel. if wanted, bind contents of textbox selecteditem well, , create converter move between model , desired string representation. alternately, , little more simply, have separate property (e.g. selecteditemtotext) takes selecteditem , stringifies somehow within viewmodel, though farther mvvm pattern.

wpf

No comments:

Post a Comment