checkbox - Bind to a property and Element value - WPF binding -
i have checkbox
binded object's property "isvalidcustomer
" , have listview
holds customers. whenever user selects customer
in list, want checkbox
checked
property set false
means "isvalidcustomer
" property set false
automatically. there way of achieving using wpf bindings?
any in regard highly appriciated.
regards
-srikanth
first make sure view's
datacontext
setviewmodel
implementsinotifypropertychanged
interface
addselectedcustomer
property hold selectedcustomer
listview
,each time
selectedcustomer
set, check value , setisvalidcustomer
property here full code :
the view model
public class customer { public string name { get; set; } public string id { get; set; } } public partial class mainwindow : window, inotifypropertychanged { private customer _selectedcustomer; public customer selectedcustomer { { return _selectedcustomer; } set { if (_selectedcustomer == value) { return; } _selectedcustomer = value; onpropertychanged(); isvalidcustomer = (_selectedcustomer == null); } } private observablecollection<customer> _listcustomers; public observablecollection<customer> listcustomers { { return _listcustomers; } set { if (_listcustomers == value) { return; } _listcustomers = value; onpropertychanged(); } } private bool _isvalidcustomer = false; public bool isvalidcustomer { { return _isvalidcustomer; } set { if (_isvalidcustomer == value) { return; } _isvalidcustomer = value; onpropertychanged(); } } public mainwindow() { initializecomponent(); } public event propertychangedeventhandler propertychanged; [notifypropertychangedinvocator] protected virtual void onpropertychanged([callermembername] string propertyname = null) { propertychangedeventhandler handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs(propertyname)); } }
and view
<stackpanel> <checkbox content="isvalidcustomer" ischecked="{binding isvalidcustomer,mode=twoway}"></checkbox> <listview itemssource="{binding listcustomers}" selecteditem="{binding selectedcustomer,mode=twoway}"></listview> </stackpanel>
Comments
Post a Comment