我有一个名字文本框,一个姓氏文本框和一个显示名称的下拉框。显示名称是可编辑的,所以用户可以输入任何他们想要的内容。但是,下拉框应该显示一系列选项。
当任意一个文本框改变时,下拉框的项源就会更新。然而,如果用户之前选择了值,那么文本属性将被清空。但是,如果用户已经键入了一个值,文本属性仍然存在。如何防止文本值被清空?
这是我的代码:
当任意一个文本框改变时,下拉框的项源就会更新。然而,如果用户之前选择了值,那么文本属性将被清空。但是,如果用户已经键入了一个值,文本属性仍然存在。如何防止文本值被清空?
这是我的代码:
<Window x:Class="MainWindow"
x:Name="MainApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="350"
Width="525">
<Grid DataContext="{Binding ElementName=MainApp}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<TextBox Grid.Row="0"
Text="{Binding FirstName}" />
<TextBox Grid.Row="1"
Text="{Binding LastName}" />
<ComboBox x:Name="MyCb" Grid.Column="1"
Grid.Row="2"
Text="{Binding TheName}"
ItemsSource="{Binding NameOptions}"
IsEditable="True" />
</Grid>
</Window>
Imports System.ComponentModel
Class MainWindow
Implements INotifyPropertyChanged
Public ReadOnly Property NameOptions As List(Of String)
Get
Dim result As New List(Of String)
result.Add(String.Format("{0} {1}", FirstName, LastName))
result.Add(String.Format("{1}, {0}", FirstName, LastName))
result.Add(String.Format("{1}, {0} MI", FirstName, LastName))
Return result
End Get
End Property
Public Property TheName As String
Private _firstName As String
Public Property FirstName As String
Get
Return _firstName
End Get
Set(value As String)
_firstName = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("FirstName"))
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("NameOptions"))
End Set
End Property
Private _lastname As String
Public Property LastName As String
Get
Return _lastname
End Get
Set(value As String)
_lastname = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("LastName"))
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("NameOptions"))
End Set
End Property
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
End Class