Note that there are some explanatory texts on larger screens.

plurals
  1. POMVVMLight Combobox Binding Silverlight4
    primarykey
    data
    text
    <p>(MVVM Light in SL4)</p> <p>My DisplayWorkOrder view has a field named WAREHOUSE_CODE. In my ViewModel, I bind to that property. If I use a textbox and set my Text="{Binding WAREHOUSE_CODE}", then open an existing WorkOrder, my textbox is populated with the value of WAREHOUSE_CODE (e.g. Lombard). If I change the textbox to a combobox, I have issues. </p> <p>The combobox is set up like so: </p> <pre><code>&lt;ComboBox Grid.Column="1" Grid.Row="1" x:Name="cboWarehouse" Margin="4" ItemsSource="{Binding Path=Warehouses}" SelectedValue="{Binding Path=WAREHOUSE_CODE, Mode=TwoWay}" /&gt; </code></pre> <p>My Warehouses property is simply (for design mode) a list that I build in the constructor of my ViewModel: </p> <pre><code>Warehouses = new List&lt;string&gt; { "Docks", "Pearl", "Lombard", "Powell", "Goose Hollow" }; </code></pre> <p>When I load a blank work order, my combobox is properly populated with this list. However, when I select an existing Work Order, my combobox is blank. Not only don't I display the selectedValue for that work order, but my ItemsSource doesn't seem to be binding anymore. </p> <p>As a sidenote, when I select an existing WorkOrder (via ChildWindow/Messaging), I overload the constructor in my view with this: </p> <pre><code>public DisplayWorkOrder(Model.BWorkOrder workOrder) { InitializeComponent(); this.DataContext = workOrder; } </code></pre> <p>If I set a breakpoint in here, workOrder.WAREHOUSE_CODE is set propertly, so I'm confused by what's happening. </p> <p>Any ideas? </p> <p><strong>EDIT for Stephan's comment</strong> So it makes sense that, by setting my DataContext to workOrder, I'm losing the context in my combobox that's set to my ViewModel. I WANT the datacontext to be set to to my ViewModel though. So currently I do this...</p> <p>In my ListWorkOrdersViewModel, after a user selects a work order to view, I use a RelayCommand (implementing iCommand interface) to send a message of type BWorkOrder, using a token "WOSelected". I also pass my object reference through the message. In my MainPage.xaml.cs (not proud of this, I need a messaging class to handle this stuff!), I subscribe to that code block:</p> <pre><code>Messenger.Default.Register&lt;NotificationMessage&lt;Model.BWorkOrder&gt;&gt;( this, nm =&gt; { if (nm.Notification == "WOSelected") { RightContent.Content = new Views.DisplayWorkOrder(nm.Content); } }); </code></pre> <p>So in this case, I'm setting the content property of my frame to an instantiated DisplayWorkOrder, overloading the constructor &amp; passing in my WO object. </p> <p>To keep the datacontext of my DisplayWorkOrderView to its ViewModel (instead of setting it to the workOrder object), I would have a property in my DisplayWorkOrderViewmOdel of type BWorkOrder, send my message from ListWorkOrderviewModel to DisplayWorkOrderViewModel, and set my workOrder object equal to the property (of type BWorkOrder), then bind the datacontext of my DisplayWorkOrder to that property? </p> <p>Hope that wasn't too confusing :) </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload