[英]How can I display data of the ListBox item in the dialog?
In my WPF program I have ListBox control: 在我的WPF程序中,我具有ListBox控件:
<ListBox x:Name="mailsListBox" SelectionChanged="mailsListBox_SelectionChanged" >
<ListBoxItem Content="..." Background="#FFF3F3F3" Margin="0,0,0,1" />
<ListBoxItem Content="..." Margin="0,0,0,1"/>
<!-- ... -->
<ListBoxItem Content="..." Background="#FFF3F3F3" Margin="0,0,0,1" />
<ListBoxItem Content="..." Margin="0,0,0,1"/>
</ListBox>
When I press on it's component, I need to show a dialog window, but here what I have: 当我按下它的组件时,我需要显示一个对话框窗口,但是这里有:
Displays the corresponding value: 显示相应的值:
public class Emails
{
public Emails()
{
AddMailsToList();
}
public List<string> mailsList = new List<string>();
public void AddMailsToList()
{
MainWindow obj = new MainWindow();
mailsList.Add(obj.mailsListBox.Items[0].ToString());
mailsList.Add(obj.mailsListBox.Items[1].ToString());
//...
mailsList.Add(obj.mailsListBox.Items[9].ToString());
// title of the mail
mailsList.Add("You have new message");
}
}
Interception click event to an element ListBox: 拦截到元素ListBox的click事件:
public partial class MainWindow : MetroWindow
{
private async void mailsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Emails obj = new Emails();
await this.ShowMessageAsync(
obj.mailsList[10],
obj.mailsList[mailsListBox.SelectedIndex]);
}
}
How can I display only data which I need in the dialog? 如何只显示对话框中需要的数据?
Edit1: 编辑1:
I thins I have a wrong code structure and I need to use ItemsSource
, but I don't know how to.. 我认为我的代码结构错误,需要使用
ItemsSource
,但我不知道该怎么做。
I don't understand the point of the Email
class, and I think you definitely have a data model structure problem. 我不了解
Email
类的要点,我认为您肯定有数据模型结构问题。
But if you just want to display the text of the selected item of your listbox, give a try to that : 但是,如果您只想显示列表框中所选项目的文本,请尝试以下操作:
private void mailsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox lb = (ListBox)sender;
if (lb != null && !string.IsNullOrEmpty(lb.SelectedItem.ToString()))
{
await this.ShowMessageAsync(
((ListBoxItem)lb.SelectedItem).Content.ToString(),
obj.mailsList[mailsListBox.SelectedIndex]);
}
}
Always check if user has selected an item. 始终检查用户是否选择了一个项目。 if you use mailsListBox.SelectedIndex and user hasn't select anything, it will return -1, and it will throw an error when you try to access to item at position "-1" of an array :)
如果您使用mailsListBox.SelectedIndex并且用户未选择任何内容,它将返回-1,并且当您尝试访问数组位置“ -1”的项目时将抛出错误:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.