c# - How to bind ReactiveList to WPF ListBox or ListView using code-behind? -
i have problem display contents of reactivelist in listbox
control in view. when seek bind via code-behind bindings (using this.onewaybind(...)
) list stays empty. using latest reactiveui version (6.1.0
). if alter binding xaml-binding , remove phone call onewaybind(...)
, list display 5 string elements.
i don't know why isn't working, simple textblock.text
-binding works expected (see code).
mainwindow.xaml:
<window x:class="view_location_test.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:view_location_test" xmlns:system="clr-namespace:system;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <stackpanel> <listbox x:name="toasterlist"> <!-- working if uncomment above line , remove onewaybind phone call in view-code: --> <!--<listbox x:name="toasterlist" itemssource="{binding toasterlist}">--> <listbox.resources> <datatemplate datatype="{x:type system:string}"> <textblock text="{binding}" /> </datatemplate> </listbox.resources> </listbox> <textblock x:name="toastername" /> </stackpanel>
mainwindow.xaml.cs:
public partial class mainwindow : window, iviewfor<viewmodel> { public mainwindow() { viewmodel = new viewmodel(); datacontext = viewmodel; initializecomponent(); // bind this.onewaybind(viewmodel, vm => vm.toasterlist, v => v.toasterlist.itemssource); this.bind(viewmodel, vm => vm.name, v => v.toastername.text); } public viewmodel viewmodel { { homecoming (viewmodel)getvalue(viewmodelproperty); } set { setvalue(viewmodelproperty, value); } } public static readonly dependencyproperty viewmodelproperty = dependencyproperty.register("viewmodel", typeof(viewmodel), typeof(mainwindow), new propertymetadata(null)); object iviewfor.viewmodel { { homecoming viewmodel; } set { viewmodel = (viewmodel)value; } } }
viewmodel.cs:
public class viewmodel : reactiveobject { public viewmodel() { toasterlist.add("toaster 1"); toasterlist.add("toaster 2"); toasterlist.add("toaster 3"); toasterlist.add("toaster 4"); toasterlist.add("toaster 5"); } private string name = "my name"; public string name { { homecoming name; } set { this.raiseandsetifchanged(ref name, value); } } private reactivelist<string> toasterlist = new reactivelist<string>(); public reactivelist<string> toasterlist { { homecoming toasterlist; } set { this.raiseandsetifchanged(ref toasterlist, value); } } }
this because you've set itemstemplate in weird way™, reactiveui thinks have no itemstemplate , setting convention-based 1 (which overkill string).
instead, set this:
<listbox x:name="toasterlist"> <listbox.itemtemplate> <datatemplate> <textblock text="{binding}" /> </datatemplate> </listbox.itemtemplate> </listbox>
c# wpf reactiveui
No comments:
Post a Comment