WPF datagrid - 选择组合项后更新属性

时间:2013-11-20 21:04:37

标签: wpf datagrid combobox

我是WPF的新手,对于我在数据网格上使用组合框的问题有疑问。从组合中选择一个项目时,绑定属性不会像我期望的那样更新。

由于我无法发布图片(这是我第一次发布到SO),我将尝试解释。如果我从组合列表中选择“D00120”,则组合将反映更改,但网格属性不会更改。

的Xaml:

<DataGridTemplateColumn Header="CPT Code" Width="75">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding PartBCombo, 
                                    RelativeSource={RelativeSource AncestorType=Window}}" 
                                    DisplayMemberPath="PartBLookup_CPTCode" 
                                    SelectedValuePath="PartBLookup_ProcedureDescription"  
                                    SelectedValue="{Binding PartBBilling_ProcedureName, UpdateSourceTrigger=PropertyChanged}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

组合项目的属性:

    Partial Public Class PartBBilling_Combobox

    Private _PartBLookup_CPTCode As String
    Public Property PartBLookup_CPTCode As String
        Get
            Return _PartBLookup_CPTCode
        End Get
        Set(value As String)
            _PartBLookup_CPTCode = value
        End Set
    End Property

    Private _PartBLookup_ProcedureDescription As String
    Public Property PartBLookup_ProcedureDescription As String
        Get
            Return _PartBLookup_ProcedureDescription
        End Get
        Set(value As String)
            _PartBLookup_ProcedureDescription = value
        End Set
    End Property

End Class

datagrid的属性:

    Private _PartBBilling_CPT As String
    Public Property PartBBilling_CPT As String
        Get
            Return _PartBBilling_CPT
        End Get
        Set(value As String)
            _PartBBilling_CPT = value
            RaisePropertyChanged("PartBBilling_CPT")
        End Set
    End Property

    Private _PartBBilling_ProcedureName As String
    Public Property PartBBilling_ProcedureName As String
        Get
            Return _PartBBilling_ProcedureName
        End Get
        Set(value As String)
            If _PartBBilling_ProcedureName <> value Then
                _PartBBilling_ProcedureName = value
                RaisePropertyChanged("PartBBilling_ProcedureName")
            End If
        End Set
    End Property

选择项目后,UI会正确显示,但网格属性不会更新。除此之外,组合框功能正常。

感谢您提供任何帮助或建议。

1 个答案:

答案 0 :(得分:0)

首先我想为您的SelectedValuePath使用一些独特的东西(用于识别正确的选择)

DisplayMemberPath="PartBLookup_CPTCode" 
SelectedValuePath="PartBLookup_CPTCode"

另外要小心并注意,您的SelectedValue将绑定到PartBCombo集合中的其中一个值,无论该类型是什么类型。因此,绑定SelectedValue的属性应该是该类型。