c# - Listbox SelectedIndices of wpf window -
how can use following code on wpf listbox window . works fine on normal win form listbox , wanted test on wpf window, im getting error saying
'system.windows.controls.listbox' not contain definition 'selectedindices' , no extension method 'selectedindices' accepting first argument of type 'system.windows.controls.listbox' found (are missing using directive or assembly reference?)
this original code have
private void listbox1_selectionchanged(object sender, selectionchangedeventargs e) { try { this.textbox1.text = people[listbox1.selectedindices[0]].name; this.connectbox.text = people[listbox1.selectedindices[0]].ipaddress; } catch { }
this example of mean "wpf mentality", differs lot archaic winforms mentality of mashing logic , ui:
<window x:class="wpfapplication4.window11" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="window11" height="300" width="300"> <dockpanel> <button dockpanel.dock="top" content="copy" command="{binding copycommand}"/> <uniformgrid columns="2"> <listbox itemssource="{binding items}" selectionmode="extended"> <listbox.itemcontainerstyle> <style targettype="{x:type listboxitem}"> <setter property="isselected" value="{binding mode=twoway, path=isselected}"/> </style> </listbox.itemcontainerstyle> </listbox> <listbox itemssource="{binding selecteditems}"/> </uniformgrid> </dockpanel> </window>
code behind:
using system.linq; using basewpfframework.mvvm; using system.collections.objectmodel; namespace wpfapplication4 { public partial class window11 { public window11() { initializecomponent(); datacontext = new listviewmodel(); } } }
viewmodel:
public class listviewmodel: viewmodelbase { private observablecollection<selectable<string>> _items; public observablecollection<selectable<string>> items { { return _items ?? (_items = new observablecollection<selectable<string>>()); } } private observablecollection<string> _selecteditems; public observablecollection<string> selecteditems { { return _selecteditems ?? (_selecteditems = new observablecollection<string>()); } } private delegatecommand _copycommand; public delegatecommand copycommand { { return _copycommand ?? (_copycommand = new delegatecommand(copy)); } } private void copy() { selecteditems.clear(); items.where(x => x.isselected).select(x => x.value).tolist().foreach(selecteditems.add); } public listviewmodel() { enumerable.range(1, 100).select(x => new selectable<string>("item" + x.tostring())).tolist().foreach(x => items.add(x)); } } public class selectable<t>: viewmodelbase { private t _value; public t value { { return _value; } set { _value = value; notifypropertychange(() => value); } } private bool _isselected; public bool isselected { { return _isselected; } set { _isselected = value; notifypropertychange(() => isselected); } } public selectable(t value) { value = value; } public selectable(t value, bool isselected): this(value) { isselected = isselected; } public override string tostring() { return value != null ? value.tostring() : string.empty; } }
just copy , paste code in file -> new -> wpf application
, see results yourself.
notice i'm using generic solution (selectable<t>
), use classes want.
also, please don't things like: this.textbox1.text = whatever
in wpf. wpf encourages separation of ui , data, , must understand ui not data in order work wpf. please leave winforms mentality behind if expect results wpf.
instead, either create proper viewmodel hold data shown in textboxes, or bind textboxes directly instance of selecteditems
in example.
as aside, off-topic comment, normal
way not winforms way anymore. recent (< 10 years
) technologies (wpf, silverlight, winrt) xaml-based , encourage use of mvvm. means winforms way old
way, not normal
way.
Comments
Post a Comment