简体   繁体   中英

Trouble binding SelectedItem in ViewModel - MVVM Light Silverlight 4

I am using SL4 with MVVM-Light Toolkit. I'm not sure what's going on here. I can't seem to find anything on the web that resolves this issue.. I have a user control that I incorporate into my MainPage.xaml using standard xmlns:local="clr-namespace:WorkOrder.Views" syntax, with a call to local:ListWorkOrdersView further down my page. Using a RelayCommand, I animate in that ListWorkOrdersView window when I click a "List Work Orders" button.

I have bound the data on ListWorkOrdersView to an ObservableCollection of test data (for blendibility). What I want to do is bind the SelectedItem of that RadGridView to a property in my ViewModel. If I place a breakpoint on the getter of the SelectedWO property, it fires when I load the control, but the breakpoint is NOT hit if I move it to the setter. Relevant code:

ListWorkOrdersView.xaml

<UserControl xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    x:Class="WorkOrder.Views.ListWorkOrdersView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;
      assembly=GalaSoft.MvvmLight.Extras.SL4" 
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
    mc:Ignorable="d"
    DataContext="{Binding ListWorkOrders, Source={StaticResource Locator}}">
  <Grid x:Name="master" 
    DataContext="{Binding ListWorkOrders, Source={StaticResource Locator}}">
      <telerik:RadGridView x:Name="gvListWO" IsReadOnly="True"
        AutoGenerateColumns="False" DataContext="{Binding wo}"
        SelectedItem="{Binding ElementName=master, 
          Path=DataContext.SelectedWO, Mode=TwoWay}"
        Grid.ColumnSpan="2"
        Width="440"
        Height="330"
       ItemsSource="{Binding}">     
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn 
           DataMemberBinding="{Binding WORK_ORDER_NUMBER}" 
           Header="Work Order Number"/>
   <telerik:GridViewDataColumn 
           DataMemberBinding="{Binding PROPOSED_BY}" Header="Proposed By"/>
   <telerik:GridViewDataColumn 
          DataMemberBinding="{Binding DATE_PROPOSED}" Header="Date Proposed"/>
      </telerik:RadGridView.Columns>
    </telerik:RadGridView>
  </Grid>
</UserControl>

Relevant ListWorkOrdersViewModel.cs(removed code for databinding the gridview itself)

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using WorkOrder.Model;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System;

namespace WorkOrder.ViewModel
{
    public class ListWorkOrdersViewModel : ViewModelBase
    {
        public const string SelectedWOPropertyName = "SelectedWO";
        private ObservableCollection<BWorkOrder> _selectedWO;
        public ObservableCollection<BWorkOrder> SelectedWO
        {
            get
            {
               return _selectedWO;
           }
           set
           {
               if (_selectedWO == value) return;
                _selectedWO = value;
                RaisePropertyChanged(SelectedWOPropertyName);
            }
       }
    }
}

Any ideas on why this isn't binding properly? I'm totally at a loss!

So the problem lied with my property. It was set as an ObservableCollection, which is what I use, for example, to populate my DataGrid or RadGridView (telerik & non telerik). When I changed it to object, I was fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM