WPF组合框绑定选定值
我正在尝试使用以下数据格式进行绑定:WPF组合框绑定选定值,wpf,vb.net,Wpf,Vb.net,我正在尝试使用以下数据格式进行绑定: Public Structure ItemBase Property ID As String Property Description As String End Structure Namespace Classes Public Class StockEntityClass Implements INotifyPropertyChanged #Region "Property Variables"
Public Structure ItemBase
Property ID As String
Property Description As String
End Structure
Namespace Classes
Public Class StockEntityClass
Implements INotifyPropertyChanged
#Region "Property Variables"
Property ID As String
Property Namee As String
Property Units As String
Property ContactID As String
Property SetCount As Integer
Property VatOnMargin As Boolean
Property Vat As Double
Property Code As String
Property _ContactNamee As String
#End Region
Public Sub New()
_IDValue = Now.ToString
_NameeValue = ""
_UnitsValue = "Pcs"
_ContactIDValue = ""
_SetCountValue = 0
_VatOnMarginValue = False
_VatValue = 14.5
_CodeValue = ""
_ContactNamee = ""
End Sub
End Class
End Namespace
在我的wpf xaml窗口(DataContext to Stock Entity变量)中,我有一个绑定到BindingList(ItemBase)的组合框,用户将选择一个项目和该项目ID作为StockEntity.ContactID的关联
以下是xaml代码:
<ComboBox Name="VendorsComboBox" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" DisplayMemberPath="Description" SelectedItem="{Binding Path=CustomerID}"/>
其中StockEntityStack包含作为datacontext绑定到stock变量的UI部分。
请告诉我如何将xaml数据绑定到它。试试这样的方法: 主窗口(XAML文件): MainViewModel文件:
Imports System.ComponentModel
Public Class MainViewModel
Implements INotifyPropertyChanged
#Region "Fields"
Private _selectedContactID As String = String.Empty
#End Region
#Region "Property Variables"
Property MySource As List(Of ItemBase) = New List(Of ItemBase)
Public Property ContactID As String
Get
Return _selectedContactID
End Get
Set(ByVal value As String)
_selectedContactID = value
OnPropertyChanged("ContactID")
End Set
End Property
#End Region
Public Sub New()
MySource.Add(New ItemBase(1, "test1"))
MySource.Add(New ItemBase(2, "test2"))
MySource.Add(New ItemBase(3, "test3"))
MySource.Add(New ItemBase(4, "test4"))
MySource.Add(New ItemBase(5, "test5"))
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Sub OnPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
Public Structure ItemBase
Public Sub New( _
ByVal _id As String,
ByVal _description As String
)
ID = _id
Description = _description
End Sub
Property ID As String
Property Description As String
End Structure
不使用此代码,对该控件使用组合框选择更改事件不是很简单吗?@surpavan您想编写与MVVM模式兼容的代码吗?如果是,您必须将SelectedItem绑定到ViewModel中的变量,在另一种情况下,您可以使用SelectionChanged事件。感谢您,我不熟悉MVVM模式,我只是一个小时间的编码器。感谢您的支持,但我肯定会尝试研究MVVM。谢谢你抽出时间。
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<ComboBox ItemsSource="{Binding MySource}"
SelectedValue="{Binding ContactID}"
DisplayMemberPath="Description"
SelectedValuePath="ID"
Width="180" Height="25"
/>
<StackPanel Orientation="Horizontal" Margin="10" >
<TextBlock Text="Selected ID:" />
<TextBlock Text="{Binding ContactID}" />
</StackPanel>
</StackPanel>
</Window>
Class MainWindow
Public Sub New()
InitializeComponent()
Me.DataContext = New MainViewModel()
End Sub
End Class
Imports System.ComponentModel
Public Class MainViewModel
Implements INotifyPropertyChanged
#Region "Fields"
Private _selectedContactID As String = String.Empty
#End Region
#Region "Property Variables"
Property MySource As List(Of ItemBase) = New List(Of ItemBase)
Public Property ContactID As String
Get
Return _selectedContactID
End Get
Set(ByVal value As String)
_selectedContactID = value
OnPropertyChanged("ContactID")
End Set
End Property
#End Region
Public Sub New()
MySource.Add(New ItemBase(1, "test1"))
MySource.Add(New ItemBase(2, "test2"))
MySource.Add(New ItemBase(3, "test3"))
MySource.Add(New ItemBase(4, "test4"))
MySource.Add(New ItemBase(5, "test5"))
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Sub OnPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
Public Structure ItemBase
Public Sub New( _
ByVal _id As String,
ByVal _description As String
)
ID = _id
Description = _description
End Sub
Property ID As String
Property Description As String
End Structure