【问题标题】:Two way datatable binding in WPFWPF中的两种方式数据表绑定
【发布时间】:2012-07-31 13:23:36
【问题描述】:

我有一个数据网格(在本例中为 C1 数据网格)绑定到我的视图模型中的一个属性。数据网格的 XAML 如下所示:

        <c1:C1DataGrid 
            AutoGenerateColumns="False"
            IsReadOnly="False"
            Margin="5" Width="auto"
            MinWidth="250"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            Name="dgNotifAssign"
            CanUserAddRows="True" 
            ItemsSource="{Binding Path=notifCodeSubs.notification_configuration}"
            >


            <c1:C1DataGrid.Columns>
                <c1:DataGridTextColumn 
                    Binding="{Binding Path=user_id}" 
                    Header="Recipient" 
                    VerticalAlignment="Stretch"
                    SortMemberPath="user_id"  
                    >

在我的视图模型中,它绑定的属性如下所示:

Public Property notifCodeSubs As dsPeruseFM
    Get
        If _notifCode Is Nothing Then
            _notifCode = New dsPeruseFM
        End If
        Return _notifCode
    End Get
    Set(ByVal value As dsPeruseFM)
        MsgBox("If you can see this, success!")
    End Set
End Property

在代码隐藏中,我创建了一个视图模型的实例并将 xaml 的数据上下文设置为该实例,相当简单......

Dim vm As New ctrlAlertNotifyVM

还有:

ctrlAlertNotifyXML.DataContext = vm

上面的配置编译和读取数据就好了。网格中填充了所有正确的数据等。当我尝试将Mode=twoway 添加到数据网格上的 ItemsSource 时,问题就出现了。此时VS2010吐出以下错误:

TwoWay 或 OneWayToSource 绑定无法对“PeruseFM.dsPeruseFM”类型的只读属性“notification_configuration”起作用。

我很确定我的所有属性都是读/写的。虽然此时的 set 命令只不过是一个消息框,但似乎我什至无法访问它。

所以问题是......以前有人遇到过这个问题吗?

更新,回答问题“notification_configuration 是什么样的?”来自六个字母的变量:

Public Function codeChanged(Optional ByVal x As String = "")

    If _notifCode Is Nothing Then
        _notifCode = New dsPeruseFM
    End If
    taNotifSubs.fillNotifSubs(notifCode:=x, dataTable:=_notifCode.notification_configuration)
    Return _notifCode
End Function

【问题讨论】:

  • notification_configuration 长什么样子?

标签: wpf vb.net binding datagrid componentone


【解决方案1】:

您向我们展示了 notifCodeSubs 是读/写的,但是,这不是您绑定到的实际属性。

从这个角度来看,错误消息是不言自明的:

...只读属性 'notification_configuration'...

因此,您不能将TwoWay 绑定作为ItemsSource 应用到该属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 2016-02-27
    • 2021-03-01
    • 1970-01-01
    • 2016-10-24
    相关资源
    最近更新 更多