![](/img/trans.png)
[英]WPF ComboBox with checkboxes and textbox with search field show text box on scroll
[英]cant show text of combobox in textbox wpf
嗨,伙計,我使用此代碼
private void combobox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
text_f1.text = combobox1.Text;
}
我的第一條記錄是“汽車”,第二條記錄是“電話”...
首先,當我選擇“汽車”時,當我選擇第二個或其他記錄時不顯示任何內容,向我顯示“汽車”
總是有點落后
出現這個問題后,我嘗試使用此代碼
text_f1.text = combobox1.DisplayMemberPath;
給我看看
"MahName"
這是錯的
我該怎么辦 ?
您可以使用ElementBinding在XAML中輕松完成此操作。
<ComboBox x:Name="comboBox"/>
<TextBlock Text="{Binding Path=SelectedItem.MahName, ElementName=comboBox}"/>
我認為您應該使用SelectedIndexChanged事件而不是SelectionChanged事件。 您的第一個代碼應與此事件一起使用。
combobox1.Text
是combobox1.Text
的可編輯文本(當選擇更改時,它會滯后一點)。 確切的解決方案取決於組合框包含哪些項目以及如何配置。
如果包含字符串,則可以編寫
text_f1.Text = (string)combobox1.SelectedItem;
如果它包含Article
類的對象(例如),則可以使用
if (combobox1.SelectedItem is Article article) {
text_f1.Text = article.MahName;
}
或者如果項目的ToString
方法已被覆蓋
if (combobox1.SelectedItem != null) {
text_f1.Text = combobox1.SelectedItem.ToString();
}
要么
text_f1.Text = combobox1.SelectedItem?.ToString();
如果沒有選定的項目,則此最新版本將null
分配給text_f1
。 參見: ? 和?[]空條件運算符
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.