在 Silverlight 3 中同步 DataGrid 和 DataForm

发布于 2024-08-28 15:01:19 字数 1681 浏览 3 评论 0原文

这几天我一直在用头撞墙,现在是时候寻求帮助了。

我在同一个 UserControl 上有一个 DataGrid 和 DataForm。我使用的是 MVVM 方法,因此 UserControl 有一个 ViewModel。该 ViewModel 有几个与此讨论相关的属性:

public ObservableCollection<VehicleViewModel> Vehicles { get; private set; }
public VehicleViewModel SelectedVehicle
{
    get { return selectedVehicle; }
    private set
    {
        selectedVehicle = value;
        OnPropertyChanged( "SelectedVehicle" );
    }
}

在 XAML 中,我定义了 DataGrid 和 DataForm,如下所示:

<data:DataGrid SelectionMode="Single"
               ItemsSource="{Binding Vehicles}"
               SelectedItem="{Binding SelectedVehicle, Mode=TwoWay}"
               AutoGenerateColumns="False"
               IsReadOnly="True">

<dataFormToolkit:DataForm CurrentItem="{Binding SelectedVehicle}" />

因此,当 DataGrid 上的 SelectedItem 发生更改时,它应该将该更改推送回 ViewModel,并且当 ViewModel引发 OnPropertyChanged,DataForm 应该使用新选择的 VehicleViewModel 的信息刷新自身。但是,SelectedVehicle 的 setter 永远不会被调用,并且在 VS 的输出窗口中我看到以下错误:

System.Windows.Data Error: ConvertBack 无法转换值 'xxxx.ViewModel.VehicleViewModel' (type 'xxxx.ViewModel.车辆视图模型')。 BindingExpression: Path='SelectedVehicle' DataItem='xxxx.ViewModel.MainViewModel' (HashCode=31664161);目标元素是“System.Windows.Controls.DataGrid”(名称=“”);目标属性是“SelectedItem”(类型“System.Object”).. System.MethodAccessException:xxxx.ViewModel.MainViewModel.set_SelectedVehicle(xxxx.ViewModel.VehicleViewModel)

听起来好像从VehicleViewModel转换为对象(或再次回来),但我很困惑为什么会这样(或者即使我的假设是正确的)。 DataGrid 中的每一行/项目都应该是一个 VehicleViewModel(因为 ItemsSource 绑定到该类型的 ObservableCollection),因此当 SelectedItem 更改时,它应该处理 VehicleViewModel 的实例。

任何见解将不胜感激。

I've been banging my head against the wall for a couple of days on this and it's time to ask for help.

I've got a DataGrid and DataForm on the same UserControl. I'm using an MVVM approach so there is a single ViewModel for the UserControl. That ViewModel has a couple of properties that are relevant to this discussion:

public ObservableCollection<VehicleViewModel> Vehicles { get; private set; }
public VehicleViewModel SelectedVehicle
{
    get { return selectedVehicle; }
    private set
    {
        selectedVehicle = value;
        OnPropertyChanged( "SelectedVehicle" );
    }
}

In the XAML I've got the DataGrid and DataForm defined as follows:

<data:DataGrid SelectionMode="Single"
               ItemsSource="{Binding Vehicles}"
               SelectedItem="{Binding SelectedVehicle, Mode=TwoWay}"
               AutoGenerateColumns="False"
               IsReadOnly="True">

<dataFormToolkit:DataForm CurrentItem="{Binding SelectedVehicle}" />

So as the SelectedItem changes on the DataGrid it should push that change back to the ViewModel and when the ViewModel raises the OnPropertyChanged the DataForm should refresh itself with the information for the newly-selected VehicleViewModel. However, the setter for SelectedVehicle is never being called and in the Output window of VS I'm seeing the following error:

System.Windows.Data Error: ConvertBack cannot convert value 'xxxx.ViewModel.VehicleViewModel' (type 'xxxx.ViewModel.VehicleViewModel'). BindingExpression: Path='SelectedVehicle' DataItem='xxxx.ViewModel.MainViewModel' (HashCode=31664161); target element is 'System.Windows.Controls.DataGrid' (Name=''); target property is 'SelectedItem' (type 'System.Object').. System.MethodAccessException: xxxx.ViewModel.MainViewModel.set_SelectedVehicle(xxxx.ViewModel.VehicleViewModel)

It sounds like it's having a problem converting from a VehicleViewModel to an object (or back again), but I'm confused as to why that would be (or even if I'm on the right track with that assumption). Each row/item in the DataGrid should be a VehicleViewModel (because the ItemsSource is bound to an ObservableCollection of that type), so when the SelectedItem changes it should be dealing with an instance of VehicleViewModel.

Any insight would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不乱于心 2024-09-04 15:01:19

public VehicleViewModel SelectedVehicle 上的 setter 是私有的,因此 DataGrid 无法使用 TwoWay 模式修改它。您的设置器需要公开。

Your setter on the public VehicleViewModel SelectedVehicle is private so the DataGrid cannot modify it using the TwoWay mode. Your setter will need to be public.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文