C# 如何在wpf中读取可编辑组合框
我正在尝试将combobox设置为可编辑 组合框具有数据绑定,该绑定使用数据库中的名称列表进行更新。 如果所需名称不在列表中,用户可以使用此组合框输入新名称(IsEditable设置为true) 此外,数据库中还有一些以前项目的记录,我希望根据项目选择显示这些记录,用户希望更新这些记录 下面是XAML代码 基于TPTypeComboBox选择C# 如何在wpf中读取可编辑组合框,c#,wpf,C#,Wpf,我正在尝试将combobox设置为可编辑 组合框具有数据绑定,该绑定使用数据库中的名称列表进行更新。 如果所需名称不在列表中,用户可以使用此组合框输入新名称(IsEditable设置为true) 此外,数据库中还有一些以前项目的记录,我希望根据项目选择显示这些记录,用户希望更新这些记录 下面是XAML代码 基于TPTypeComboBox选择 <ComboBox Height="23" HorizontalAlignment="Left" Margin="285,68,0,0" Name=
<ComboBox Height="23" HorizontalAlignment="Left" Margin="285,68,0,0" Name="TPTypeComboBox" VerticalAlignment="Top" Width="119" ItemsSource="{Binding}" DisplayMemberPath="name" SelectionChanged="TPTypeComboBox_SelectionChanged"/>
这是XAML代码(用于可编辑组合框)
但我得到的“projectdusedcombotxtbox”总是空的
任何建议/链接,如何阅读此文本值
关于您应该使用来向代码隐藏而不是名称公开该元素。您读取的代码是在SelectionChanged处理程序中还是在另一个处理程序中?我正在另一个处理程序中读取(单击按钮)您的代码似乎工作得很好。你能发布更多细节(或更多代码)吗?@Il-Vic,编辑问题。
private void TPTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ProjectIDUsedComboTxtBox.DataContext = DBConnector.IsTPRecordsExists(); // ISTPRecordsEsixts returns DataTable
}
<ComboBox Height="22" HorizontalAlignment="Left" Margin="131,144,0,0" Name="ProjectIDUsedComboTxtBox" VerticalAlignment="Top" Width="271" IsEditable="True"
ItemsSource="{Binding}" DisplayMemberPath="project_name_used" SelectionChanged="ProjectIDUsedComboTxtBox_SelectionChanged" />
<Button Content="Update" Height="24" HorizontalAlignment="Left" Margin="227,596,0,0" Name="UpdateTPRecordButton" VerticalAlignment="Top" Width="74" Click="UpdateTPRecordButton_Click" />
if (ProjectIDUsedComboTxtBox != null)
{
var selItem = (DataRowView)ProjectIDUsedComboTxtBox.SelectedItem;
if (selItem != null)
{
var temp = selItem.Row;
var value = (string)temp.ItemArray.GetValue(0);
}
else
{
var prjIDUsed = ProjectIDUsedComboTxtBox.Text.Trim();
}
}