wpf - Best practice for binding properties of controls within a DataTemplate? -


i see there have been lot of question asked on years, how best bind data within datatemplate, there best practice? in case want memorycopybtn copy text of selected memorylistitem textbox, can work on in viewmodel.

i can find listview using findname, show null listviewitem on pageload.

i can put in memory model page, don't think that's best practice.

i see that various options walking visual code tree, want during runtime, way?

what options? thank you.

<listview x:name="clipboardlist"                       xmlns:m="using:quickieedit.models"                       itemssource="{x:bind viewmodel.memoryitems}">                 <listview.itemtemplate>                     <datatemplate x:datatype="m:memoryitem">                             <stackpanel orientation="horizontal">                                 <button x:name="memorycopybtn"                                              content="copy"                                              click="how copy selected                                              memorylistitem.text?"/>                                  <textbox x:name="memorylistitem"                                                text="{x:bind memory, mode=twoway}">                                </textbox>                                                              </stackpanel>                     </datatemplate>                 </listview.itemtemplate> </listview> 

if understand correctly - want copy text of textbox located button click, not selected\focused textbox (otherwise not make sense, because have copy button each item in list view). if so, use command:

    <listview.itemtemplate>         <datatemplate x:datatype="m:memoryitem">             <stackpanel orientation="horizontal">                 <button x:name="memorycopybtn"                         content="copy"                         command="{binding copycommand}"                         commandparameter="{binding elementname=memorylistitem, path=text}" />                 <textbox x:name="memorylistitem"                          text="{x:bind memory, mode=twoway}"></textbox>             </stackpanel>         </datatemplate>     </listview.itemtemplate> 

and in memoryitem:

public class memoryitem {     public memoryitem() {         copycommand = new relaycommand(copy);     }              public string memory { get; set; }     public icommand copycommand { get; private set; }      private void copy(object argument)     {         var text = (string)argument;         console.writeline(text);     } } 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -