麻烦在ViewModel中绑定SelectedItem - MVVM Light Silverlight 4

时间:2011-01-25 03:15:59

标签: c# silverlight mvvm-light

我正在使用SL4和MVVM-Light Toolkit。我不确定这里发生了什么。我似乎无法在网上找到解决此问题的任何内容。我有一个用户控件,我使用标准xmlns:local =“clr-namespace:WorkOrder.Views”语法,通过调用将其合并到我的MainPage.xaml中到本地:ListWorkOrdersView进一步向下我的页面。使用RelayCommand,当我单击“List Work Orders”按钮时,我在ListWorkOrdersView窗口中设置动画。

我已将ListWorkOrdersView上的数据绑定到测试数据的ObservableCollection(用于混合)。我想要做的是将RadGridView的SelectedItem绑定到我的ViewModel中的属性。如果我在SelectedWO属性的getter上放置一个断点,它会在我加载控件时触发,但如果我将它移动到setter,则不会触发断点。相关代码:

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>

相关ListWorkOrdersViewModel.cs(删除了用于数据绑定gridview本身的代码)

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);
            }
       }
    }
}

关于为何没有正确绑定的任何想法?我完全不知所措!

1 个答案:

答案 0 :(得分:0)

所以这个问题与我的财产有关。它被设置为ObservableCollection,这是我使用的,例如,填充我的DataGrid或RadGridView(telerik&amp; non telerik)。当我把它改成对象时,我很好。