C# WPF: Place UserControl in DataGridRow -


i'm creating wpf application in c#. in window there datagrid. in grid 2 columns. first column contains strings. in second column want display usercontrol created.

the usercontrol (called: productcontrol) consists of 3 buttons , 3 textboxes.

this xaml code control:

<usercontrol x:class="cards.productcontrol"          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"           mc:ignorable="d" height="95" width="273"> <grid margin="-23,0,0,0">     <grid.rowdefinitions>         <rowdefinition height="10*"/>         <rowdefinition height="24*"/>         <rowdefinition height="29*"/>         <rowdefinition height="32*"/>     </grid.rowdefinitions>     <button x:name="btnsp_p" content="sp/p" horizontalalignment="left" margin="215,3,0,0" verticalalignment="top" width="75" grid.row="2"/>     <button x:name="btnnm_m" content="nm/m" horizontalalignment="left" margin="215,0,0,0" verticalalignment="top" width="75" grid.row="1"/>     <button x:name="btnhp" content="hp" horizontalalignment="left" margin="215,4,0,0" verticalalignment="top" width="75" grid.row="3"/>     <textbox x:name="txtnm_m" horizontalalignment="left" height="23" margin="27,0,0,0" textwrapping="wrap" text="textbox" verticalalignment="top" width="183" grid.row="1"/>     <textbox x:name="txtsp_p" horizontalalignment="left" height="23" margin="27,4,0,0" textwrapping="wrap" text="textbox" verticalalignment="top" width="183" grid.row="2"/>     <textbox x:name="txthp" horizontalalignment="left" height="23" margin="27,3,0,0" textwrapping="wrap" text="textbox" verticalalignment="top" width="183" grid.row="3"/>  </grid> 

this c# code control:

public partial class productcontrol : usercontrol     {         public string nm_m { get; protected set; }         public string sp_p { get; protected set; }         public string hp { get; protected set; }          public productcontrol()         {             initializecomponent();         }          public void set(string nm_m, string sp_p, string hp)         {             this.nm_m = nm_m;             this.sp_p = sp_p;             this.hp = hp;              txthp.text = hp;             txtnm_m.text = nm_m;             txtsp_p.text = sp_p;         }     } 

i have 2 classes contains data need displayed in datagrid:

class dataitems {     public list<dataitemcard> items = new list<dataitemcard>(); } class dataitemcard {     public string reference { get; set; }     public productcontrol products { get; set; } } 

the instance of dataitems used itemssource datagrid. string displayed correctly, in second column stands type: 'cards.productcontrol'.

the datagrid declared in xaml file as:

<datagrid x:name="griddisplaycards" horizontalalignment="left" margin="10,37,0,0" verticalalignment="top" rendertransformorigin="3.046,4.843" height="273" width="284"> 

my question: how can display control in cell?

thank , external links. works now, problem assembly. used: xmlns:controls="clr-namespace:outpost_buy_in_single_cards;assembly=outpost_buy_in_single_cards" needed be:

xmlns:controls="clr-namespace:outpost_buy_in_single_cards" 

you need use datagridtemplatecolumn when want display custom control within datagrid ...

https://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtemplatecolumn%28v=vs.110%29.aspx

you need add xmlns directive xmlns:controls="clr-namespace:mycontrols;assembly=mycontrols" window, , reference control this:

       <datagridtemplatecolumn>             <datagridtemplatecolumn.celltemplate>                 <datatemplate>                     <mycontrols:productcontrol /><!--you need add binding expressions productcontrol element-->                 </datatemplate>             </datagridtemplatecolumn.celltemplate>         </datagridtemplatecolumn> 

this existing question should iron out binding problems have ...

how bind user control using datagridtemplatecolumn failed in wpf


Comments