Wpf DataGridTemplateColumn ComboBox 在选择一个项目后显示文本
Wpf DataGridTemplateColumn ComboBox displayed text after selected an item
我有一个使用 DataGridTemplateColumn 的数据网格,在 CellEditingTemplate 中使用了一个组合框。一切正常,直到 selected 值。我花了很多时间试图找出如何使这项工作。基本上,用户 select 从组合框中输入一个文本项(显示文本),数据库所需的值存储为 selected 值,用户只能看到显示文本。它现在的行为方式是用户在打开 select 项目的组合框时只能看到显示文本。但是在项目 selected 之后,显示 selected 值。下面是我的 xaml 和 vb 代码和屏幕截图。
从组合框中选择一个项目显示显示文本,这很好。
在用户 select 一个项目之后,selected 值是;这是我不想要的。
<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>
Class 获取处置代码:
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 class:
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
您在 Disposition
列中观察到 DispositionID
,因为您有 CellTemplate
和 txtDisposition
显示此内容。
要显示 DefectDesc
,您可以将 ValueConverter
添加到将 id 转换为 DefectDesc
的 txtDisposition
绑定。
或者您可以在 QC
class 中添加 DefectDesc
属性 以显示该描述。您应该在 DispositionID
属性 setter 中添加更新 DefectDesc
的代码。并将 txtDisposition
绑定更改为 DefectDesc
.
我有一个使用 DataGridTemplateColumn 的数据网格,在 CellEditingTemplate 中使用了一个组合框。一切正常,直到 selected 值。我花了很多时间试图找出如何使这项工作。基本上,用户 select 从组合框中输入一个文本项(显示文本),数据库所需的值存储为 selected 值,用户只能看到显示文本。它现在的行为方式是用户在打开 select 项目的组合框时只能看到显示文本。但是在项目 selected 之后,显示 selected 值。下面是我的 xaml 和 vb 代码和屏幕截图。
从组合框中选择一个项目显示显示文本,这很好。
在用户 select 一个项目之后,selected 值是;这是我不想要的。
<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>
Class 获取处置代码:
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 class:
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
您在 Disposition
列中观察到 DispositionID
,因为您有 CellTemplate
和 txtDisposition
显示此内容。
要显示 DefectDesc
,您可以将 ValueConverter
添加到将 id 转换为 DefectDesc
的 txtDisposition
绑定。
或者您可以在 QC
class 中添加 DefectDesc
属性 以显示该描述。您应该在 DispositionID
属性 setter 中添加更新 DefectDesc
的代码。并将 txtDisposition
绑定更改为 DefectDesc
.