c# - How to change WPF control bindings at runtime -
my form allows me add , edit record (at different times).
currently have save button commits changes database:
<page.datacontext> <viewmodels:recordsviewmodel /> </page.datacontext> <grid> <button name="btnsave" content="save" comand="{binding addrecordcommand}" /> </grid>
when click in datagrid edit record, record data fetched database , loaded form fields per allocated bindings.
unfortunately, doesn't change binding on save button, i've tried in code:
// datagrid edit button clicked private void btnedit_click(object sender, routedeventargs e) { btnsave.setbinding(button.commandproperty, new binding { source = this.datacontext, path = new propertypath("updaterecordcommand") }); btnsave.commandparameter = new binding("id"); glist.visibility = visibility.collapsed; gaddedit.visibility = visibility.visible; }
this, unfortunately, not work , binding remains unchanged save new record database.
how can change button's binding it'll update record instead of adding new one?
if checking id!=0
private void btnedit_click(object sender, routedeventargs e) { if (id != 0) { // update code - not touch binding // should call code class, // it's better separate out concerns within project. } else { // when there error - display message // user. load specific page, reload page. } }
the far better way manage user changing between adding or updating record have these 2 functions separated. program flow , ui. far better separate creation or edit functionality.
if need check if id exists , not - can load creation page.
Comments
Post a Comment