我有UserControl包含ComboBox和TextBox:
<ComboBox x:Name="cmbId" SelectionChanged="cmbId_SelectionChanged" />
<TextBox x:Name="txb1" Visibility="Hidden" Text="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged}" />
我已将值加载到ComboBox:
public MyDataView()
{
InitializeComponent();
MyDBViewDataContext mydata = new MyDBViewDataContext();
cmbId.ItemsSource = (from q in mydata.test_views
select q.Id).ToList();
}
当用户在ComboBox上选择项目时,TextBox应显示值:
private void cmbId_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int myId = 0;
Int32.TryParse(cmbId.Text, out myId);
txb1.Visibility = Visibility.Visible;
MyDBViewDataContext myDataList = new MyDBViewDataContext();
var queryAll = (from q in myDataList.test_views
where q.Id.Equals(myId)
select q);
}
但它不起作用。缺少什么或我做错了什么?
答案 0 :(得分:2)
如果Id
和FirstName
都是实体类的属性,则可以将ItemsSource
的{{1}}属性设置为ComboBox
并绑定IEnumerable<YourEntityType>
Text
的{{1}} TextBox
属性的FirstName
属性:
SelectedItem
然后根本不需要处理ComboBox
事件。
另一个选项是在事件处理程序中实际设置public MyDataView()
{
InitializeComponent();
MyDBViewDataContext mydata = new MyDBViewDataContext();
cmbId.ItemsSource = (from q in mydata.test_views
select q).ToList();
}
<ComboBox x:Name="cmbId" DisplayMemberPath="Id"/>
<TextBox x:Name="txb1" Visibility="Hidden" Text="{Binding Path=SelectedItem.FirstName, UpdateSourceTrigger=PropertyChanged, ElementName=cmbId}" />
的{{1}}属性:
SelectionChanged
答案 1 :(得分:1)
您已将文本框与属性绑定,但我无法在您的代码中看到您是否正在设置您的媒体资源。将属性设置为所选项时,文本框将显示所选名称。