c# - WPF ListBox selection does not work -
i have listbox few items, , need able click them. problem is, selectionchanged
event doesn't fired when click on item's text, if click on blank part. i'm quite new wpf, , don't understand why happening.
xaml:
<listbox name="lboxvouchers" borderthickness="0" fontsize="15" selectionchanged="lboxvouchers_selectionchanged"> <listbox.itemtemplate> <datatemplate> <listboxitem content="{binding name}" /> </datatemplate> </listbox.itemtemplate> </listbox>
handler:
private void lboxvouchers_selectionchanged(object sender, selectionchangedeventargs e) { if (e.addeditems.count > 0) messagebox.show("you selected " + e.addeditems[0]); }
i'm binding list of objects in code via lboxvouchers.itemssource
property, , show up. each object has name
property, of course.
i've tried setting isenabled on listbox , items, both in code , xaml, doesn't help.
any comments better ways in wpf welcome.
if want show name property define listbox this:
<listbox name="lboxvouchers" borderthickness="0" fontsize="15" selectionchanged="lboxvouchers_selectionchanged" displaymemberpath="name" />
if put items on observablecollection on code-behind, can pass databinding xaml:
<listbox name="lboxvouchers" borderthickness="0" fontsize="15" selectionchanged="lboxvouchers_selectionchanged" displaymemberpath="name" itemssource={binding path=items}" />
and on code behind should have like:
observablecollection<object> items {get; set}
about handler, this:
private void lboxvouchers_selectionchanged(object sender, selectionchangedeventargs e) { if (((listbox)sender).selecteditem != null) messagebox.show("you selected " + (listbox)sender).selecteditem); }
Comments
Post a Comment