c# - An ObservableCollection of a DependencyProperty is returning null -
i in saga creating custom control datagrid , 2 buttons, 1 add , remove elements datagrid. thing picture below.
.
right can add elements , bind itemssorce of datagrid directly collection of viewmodel exposing dependecyproperty. here code in question have made here. please consider @sandesh corrections.
now want implement remove button adding behaviour should same every usage: remove selected row of datagrid. add above code code behind of customdatagrid.xaml:
private void removebuttonclick(object sender, routedeventargs e) { var selecteditem = customdatagrid.selectedvalue; if (selecteditem != null && colection != null) { colection.remove(selecteditem); } }
but when press remove button colection return null , nothing happens.
thanks help.
you mixing code collections. take selected item customdatagrid.selectedvalue
, try remove collection
collection. try remove data binding property customdatagrid.selecteditem
property , removing instead:
<datagrid name="customdatagrid" itemssource="{binding collection}" selecteditem="{binding yournewselecteditemproperty}" ... />
...
colection.remove(yournewselecteditemproperty);
update >>>
to clarify further:
you can remove item collection if exact item exists in collection. in case, using selectedvalue
property returns (from linked page):
the value of selecteditem, obtained using
selectedvaluepath
.
note part highlighted. not return actual selected item. in fact, depending on value of selectedvaluepath
property, return property of selected item. therefore, should use selecteditem
property, or object data bound property instead.
Comments
Post a Comment