Wpf DataGridTemplateColumn ComboBox在选择项目后显示文本

时间:2016-01-27 22:20:10

标签: wpf data-binding wpfdatagrid

我有一个使用DataGridTemplateColumn的数据网格,而在CellEditingTemplate中有一个组合框。一切正常,直到选择一个值。我花了很多时间试图弄清楚如何使这项工作。基本上,用户从组合框中选择文本项(显示文本),数据库所需的值被存储为所选值,并且用户只能看到显示文本。它现在的行为方式是用户在打开组合框以选择项目时只能看到显示文本。但是在选择项目后,将显示所选的值。下面是我的xaml和vb代码和屏幕截图。

从组合框中选择一个项目会显示显示文字,这很好。

enter image description here

用户选择项目后,而不是显示的显示文本,所选值为;这是我不想要的。

enter image description here

<Window.Resources>
    <ObjectDataProvider x:Key="dispProvider" ObjectType="{x:Type local:QCDisposition}"
                        MethodName="GetDispositions"></ObjectDataProvider>
    <ObjectDataProvider x:Key="defectDeptProvider" ObjectType="{x:Type local:QCDefectDept}"
                        MethodName="GetDepts"></ObjectDataProvider>

</Window.Resources>

<DataGridTemplateColumn x:Name="DispositionCol" Header="Disposition">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="txtDisposition" Text="{Binding DispositionID, UpdateSourceTrigger=PropertyChanged}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox x:Name="cboDisposition" DisplayMemberPath="DefectDesc" SelectionChanged="DispositionChanged" ItemsSource="{Binding Source={StaticResource dispProvider}}" SelectedItem="DispositionID" SelectedValuePath="DispositionID" SelectedValue="DispositionID" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
                <DataGridTemplateColumn.CellStyle>
                    <Style>
                        <Setter Property="TextBlock.MinWidth"  Value="100" />
                        <Setter Property="TextBlock.TextAlignment" Value="Left" />
                    </Style>
                </DataGridTemplateColumn.CellStyle>
            </DataGridTemplateColumn>

获取处理代码的类:

Public Class QCDisposition
Implements INotifyPropertyChanged

Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

'Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
    If Not PropertyChangedEvent Is Nothing Then
        RaiseEvent PropertyChanged(Me, e)
    End If
End Sub

Private _dispositionID As Integer
Public Property QCDispID() As Integer
    Get
        Return _dispositionID
    End Get
    Set(ByVal value As Integer)
        _dispositionID = value
        OnPropertyChanged(New PropertyChangedEventArgs("QCDispositionID"))
    End Set
End Property

Private _defectCode As String
Public Property DefectDesc() As String
    Get
        Return _defectCode
    End Get
    Set(ByVal value As String)
        _defectCode = value
        OnPropertyChanged(New PropertyChangedEventArgs("DefectCode"))
    End Set
End Property

Public Sub New(dispositionID As Integer, defectCode As String)
    Me.QCDispID = dispositionID
    Me.DefectDesc = defectCode
End Sub

Public Shared Function GetDispositions() As ICollection(Of QCDisposition)
    Try
        Dim m_Dispositions As ObservableCollection(Of QCDisposition) = New ObservableCollection(Of QCDisposition)()

        Dim db1 As New QCDispositionDataContext

        Dim disp = From go In db1.QCDispositionCodes
                       Select go

        For Each g In disp
            m_Dispositions.Add(New QCDisposition(g.QCDispositionID, g.DefectCode))
        Next

        Return m_Dispositions
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Unhandled Error", MessageBoxButton.OK, MessageBoxImage.Error)
        Return Nothing
    End Try
End Function
End Class

QC课程:

Imports System.ComponentModel
Imports System.Collections.ObjectModel

Public Class QCItem
Inherits ObjectBase
Implements INotifyPropertyChanged

Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
    If Not PropertyChangedEvent Is Nothing Then
        RaiseEvent PropertyChanged(Me, e)
    End If
End Sub

Private _salesOrder As Integer
Public Property SalesOrder() As Integer
    Get
        Return _salesOrder
    End Get
    Set(value As Integer)
        _salesOrder = value

        OnPropertyChanged(New PropertyChangedEventArgs("fldSalesOrder"))
    End Set
End Property

Private _productRecordNo As Integer
Public Property ProductRecordNumber() As Integer
    Get
        Return _productRecordNo
    End Get
    Set(value As Integer)
        _productRecordNo = value

        OnPropertyChanged(New PropertyChangedEventArgs("fldProductRecordNumber"))
    End Set
End Property

Private _taskNumber As Integer
Public Property TaskNumber() As Integer
    Get
        Return _taskNumber
    End Get
    Set(value As Integer)
        _taskNumber = value

        OnPropertyChanged(New PropertyChangedEventArgs("fldTaskNumber"))
    End Set
End Property

Private _qcDesc As String
Public Property Description() As String
    Get
        Return _qcDesc
    End Get
    Set(value As String)
        _qcDesc = value

        OnPropertyChanged(New PropertyChangedEventArgs("fldDescription"))
    End Set
End Property

Private _enteredBy As String
Public Property EnteredBy() As String
    Get
        Return _enteredBy
    End Get
    Set(value As String)
        _enteredBy = value

        OnPropertyChanged(New PropertyChangedEventArgs("fldEnteredBy"))
    End Set
End Property

Private _enteredDate As String
Public Property EnteredDate() As String
    Get
        Return Convert.ToString(Convert.ToDateTime(_enteredDate).ToShortDateString)
    End Get
    Set(value As String)
        _enteredDate = value

        OnPropertyChanged(New PropertyChangedEventArgs("fldEnteredDate"))
    End Set
End Property

Private _completedDate As Nullable(Of Date)
Public Property CompletedDate() As Nullable(Of Date)
    Get
        Return _completedDate
    End Get
    Set(value As Nullable(Of Date))
        _completedDate = value
        OnPropertyChanged(New PropertyChangedEventArgs("fldCompleteDate"))
    End Set
End Property

Private _dispositionID As Integer
Public Property DispositionID() As Integer
    Get
        Return _dispositionID
    End Get
    Set(value As Integer)
        _dispositionID = value

        OnPropertyChanged(New PropertyChangedEventArgs("QCDispositionID"))
    End Set
End Property

Private _deptCodeID As Decimal
Public Property DeptCodeID() As Decimal
    Get
        Return _deptCodeID
    End Get
    Set(value As Decimal)
        _deptCodeID = value

        OnPropertyChanged(New PropertyChangedEventArgs("QCDeptCodeID"))
    End Set
End Property

Public Sub New(ByVal salesOrder As Integer, ByVal productRecNo As Integer, ByVal taskNumber As Integer, ByVal desc As String,
               ByVal enteredBy As String, enteredDate As Nullable(Of Date), ByVal completedDate As Nullable(Of Date), ByVal dispositionID As Integer, ByVal deptCodeID As Decimal)

    Try
        Me.SalesOrder = salesOrder
        Me.ProductRecordNumber = productRecNo
        Me.TaskNumber = taskNumber
        Me.Description = desc
        Me.EnteredBy = enteredBy
        Me.EnteredDate = enteredDate
        Me.CompletedDate = completedDate
        Me.DispositionID = dispositionID
        Me.DeptCodeID = deptCodeID
    Catch ex As Exception
        MessageBox.Show(ex.Message, "New_Event", MessageBoxButton.OK, MessageBoxImage.Error)
    End Try
End Sub
End Class

Public Class QCItems
Implements INotifyPropertyChanged

Private _qcItem As ICollection(Of QCItem)
Public Property QCItem As ICollection(Of QCItem)
    Get
        Return _qcItem
    End Get
    Set(value As ICollection(Of QCItem))
        _qcItem = value

        OnPropertyChanged(New PropertyChangedEventArgs("QCItem"))
    End Set
End Property

Public Sub New()
    Me.QCItem = New ObservableCollection(Of QCItem)
End Sub

Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
    If Not PropertyChangedEvent Is Nothing Then
        RaiseEvent PropertyChanged(Me, e)
    End If
End Sub
End Class

答案(感谢bars222):

<TextBlock x:Name="txtDisposition" Text="{Binding DispositionID, Converter={StaticResource qcDispCvrt}, UpdateSourceTrigger=PropertyChanged}" />

Public Class QCDescConverter
Implements IValueConverter

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
    Try
        If value IsNot Nothing AndAlso value.ToString().Length > 0 Then
            Dim iVal As Integer = System.Convert.ToInt32(value)
            Dim util As New DBUtil
            Dim returnVal As String

            returnVal = util.GetDefectDesc(iVal)

            Return returnVal
        End If

        Return Nothing
    Catch ex As Exception
        Return Nothing
        MessageBox.Show(ex.Message, "Insert Note Failed", MessageBoxButton.OK, MessageBoxImage.Error)
    End Try
End Function

Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
    Throw New NotImplementedException()
End Function
End Class

1 个答案:

答案 0 :(得分:1)

您在DispositionID列中观察到Disposition,因为CellTemplate显示了txtDisposition DefectDesc

要显示ValueConverter,您可以将txtDisposition添加到将{id}转换为DefectDesc的{​​{1}}绑定。

或者您可以在DefectDesc类中添加QC属性以显示该描述。您应该在DispositionID属性设置器中添加更新DefectDesc的代码。并将txtDisposition绑定更改为DefectDesc