在 ViewModel 中绑定 SelectedItem 时出现问题 - MVVM Light Silverlight 4
我正在使用 SL4 和 MVVM-Light Toolkit。我不确定这里发生了什么事。我似乎在网络上找不到任何可以解决此问题的内容。我有一个用户控件,我使用标准 xmlns:local="clr-namespace:WorkOrder.Views" 语法将其合并到我的 MainPage.xaml 中,并调用到本地:ListWorkOrdersView 进一步向下我的页面。使用 RelayCommand,当我单击“列出工单”按钮时,我会在 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);
}
}
}
}
关于为什么这不能正确绑定的任何想法?我完全不知所措了!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以问题出在我的财产上。它被设置为 ObservableCollection,我用它来填充我的 DataGrid 或 RadGridView(telerik 和非 telerik)。当我把它改为反对时,我就很好了。
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.