如何在ListView中托管此ComboBox以更新我的集合?

时间:2022-05-30 07:28:06

I have a ComboBox hosted in a ListView and I need changes in the CombBox to update the supporing class that the ListView is bound to.

我有一个在ListView中托管的ComboBox,我需要更改CombBox以更新ListView绑定的支持类。

Here is my DataTemplate

这是我的DataTemplate

<DataTemplate x:Key="Category">
    <ComboBox IsSynchronizedWithCurrentItem="False" 
              Style="{StaticResource DropDown}" 
              ItemsSource="{Binding Source={StaticResource Categories}}"
              SelectedValuePath="Airport"
              SelectedValue="{Binding Path=Category}"
              />
    </DataTemplate>

This is the Listview. The ItemSource for the ListView is a collection of Airports and is set in code behind, and has a property called Category that I need the combobox to update.

这是Listview。 ListView的ItemSource是一个机场的集合,在后面的代码中设置,并有一个名为Category的属性,我需要组合框进行更新。

<ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Airport" Width="100" />
                <GridViewColumn Header="Category" Width="100"  CellTemplate="{StaticResource Category}" />
            </GridView>
        </ListView.View>

2 个解决方案

#1


Why have you set SelectedValuePath in your ComboBox? It's difficult to say without seeing your data structures, but that doesn't look right to me.

为什么要在ComboBox中设置SelectedValuePath?没有看到你的数据结构很难说,但这对我来说并不合适。

#2


Here is the data that supports the ComboBox and ListView.

这是支持ComboBox和ListView的数据。

Imports System.Collections.ObjectModel

Class Window1

Public Airports As New ObservableCollection(Of Airport)
Public Sub New()
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    '*************************
    'Dummy data for testing


    Dim anAirports As New Airport
    anAirports.Name = "ABC"
    anAirports.Category = "AA"

    Airports.Add(anAirports)

    anAirports = New Airport
    anAirports.Name = "DEF"
    anAirports.Category = "BB"

    Airports.Add(anAirports)
    '*************************
    'Bind the airports to the list for display
    lstCategories.ItemsSource = Airports

End Sub

End Class

Public Class Airport

公共级机场

''' <summary>
''' Name of the Airport
''' </summary>
''' <remarks></remarks>
Private mName As String
Public Property Name() As String
    Get
        Return mName
    End Get
    Set(ByVal value As String)
        mName = value
    End Set
End Property

''' <summary>
''' Describes the type airport and is selected from a combobox
''' </summary>
''' <remarks></remarks>
Private mCategory As String
Public Property Category() As String
    Get
        Return mCategory
    End Get
    Set(ByVal value As String)
        mCategory = value
    End Set
End Property

End Class

''' ''' Items that are to be displayed in the ComboBox ''' ''' Public Class Categories

''''''要在ComboBox'''''公共类别中显示的项目

Inherits ObservableCollection(Of String)

Public Sub New()
    Me.Add("AA")
    Me.Add("BB")

End Sub

End Class

#1


Why have you set SelectedValuePath in your ComboBox? It's difficult to say without seeing your data structures, but that doesn't look right to me.

为什么要在ComboBox中设置SelectedValuePath?没有看到你的数据结构很难说,但这对我来说并不合适。

#2


Here is the data that supports the ComboBox and ListView.

这是支持ComboBox和ListView的数据。

Imports System.Collections.ObjectModel

Class Window1

Public Airports As New ObservableCollection(Of Airport)
Public Sub New()
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    '*************************
    'Dummy data for testing


    Dim anAirports As New Airport
    anAirports.Name = "ABC"
    anAirports.Category = "AA"

    Airports.Add(anAirports)

    anAirports = New Airport
    anAirports.Name = "DEF"
    anAirports.Category = "BB"

    Airports.Add(anAirports)
    '*************************
    'Bind the airports to the list for display
    lstCategories.ItemsSource = Airports

End Sub

End Class

Public Class Airport

公共级机场

''' <summary>
''' Name of the Airport
''' </summary>
''' <remarks></remarks>
Private mName As String
Public Property Name() As String
    Get
        Return mName
    End Get
    Set(ByVal value As String)
        mName = value
    End Set
End Property

''' <summary>
''' Describes the type airport and is selected from a combobox
''' </summary>
''' <remarks></remarks>
Private mCategory As String
Public Property Category() As String
    Get
        Return mCategory
    End Get
    Set(ByVal value As String)
        mCategory = value
    End Set
End Property

End Class

''' ''' Items that are to be displayed in the ComboBox ''' ''' Public Class Categories

''''''要在ComboBox'''''公共类别中显示的项目

Inherits ObservableCollection(Of String)

Public Sub New()
    Me.Add("AA")
    Me.Add("BB")

End Sub

End Class