【问题标题】:How can I use Listbox items with WPF?如何在 WPF 中使用 Listbox 项目?
【发布时间】:2023-04-01 22:52:01
【问题描述】:

我正在努力学习 Wpf。 当程序运行时,它给了我“没有列表框源”错误。 我正在进行 Wpf 设计,但我才刚刚开始。

我在外部添加到列表框的功能,我如何将它们显示为源。我对此一无所知。我想我已经研究了 2 个小时,但我没有找到任何答案。我的英语有些问题。我希望我不会打扰你。我的代码的所有详细信息如下。 提前感谢您的帮助。

//Note : My Class : (public partial class MainWindow : Window)
public void btnListAdd_Click(object sender, RoutedEventArgs e)
        {           
            CronList1.Items.Clear();     // ListBox      <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
            OpenFileDialog f = new OpenFileDialog();

            if (f.ShowDialog().HasValue == true)
            {
                List<string> lines1 = new List<string>();
                using (StreamReader r = new StreamReader(f.OpenFile()))
                {

                    string line;
                    while ((line = r.ReadLine()) != null)
                    {

                        CronList1.Items.Add(line); // ListBox      <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 


                    }
                }
            }
        }

2:然后我尝试读取 CronList 中的所有内容。我在课堂上运行该方法。

CronEvent cronEvent = new CronEvent();
Task.Run(cronEvent.Cron1);

3:我的代码不运行!

public async Task Cron1()
        { 
            int sayix = 1;
            while (true)
            {
                try
                {

                 (HttpWebRequest) rq WebRequest.Create(mainWindow.CronList1.Items[sayix].ToString());
                    rq .Proxy = WebRequest.GetSystemWebProxy();
                    rq .AllowAutoRedirect = false;
                    rq .Timeout = 10000;

                    HttpWebResponse rply= (HttpWebResponse)rq.GetResponse();
                    StreamReader streamReader = new StreamReader(rply.GetResponseStream());
                    rply.Close();
                    streamReader.Close();

                    mainWindow.CronList1.SelectedIndex = sayix;

                    sayix++;
                    if (sayix == mainWindow.CronList1.Items.Count)
                    {
                        sayix = 0;
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }

                await Task.Delay(TimeSpan.FromSeconds(Convert.ToDouble(mainWindow.txtTime.Text)));

            }
        }

WPF 列表框代码

<ListBox Name="CronList1" Height="390" Margin="2,7,4,0" VerticalAlignment="Top" BorderBrush="Red" Cursor="Arrow" IsSynchronizedWithCurrentItem="False" BorderThickness="1" ClipToBounds="True" SnapsToDevicePixels="True" Grid.Row="1" Grid.RowSpan="2" Grid.Column="1">

                        <ListBox.ItemBindingGroup>
                            <BindingGroup/>
                        </ListBox.ItemBindingGroup>
                        <ListBox.Effect>
                            <hc:BrightnessEffect/>
                        </ListBox.Effect>


                    </ListBox>

【问题讨论】:

    标签: c# wpf listbox


    【解决方案1】:

    我建议在 ViewModel 中使用“数据绑定”以使代码更具可读性。

    您创建一个类(例如 MainViewModel)并在其中创建一个 ObservableCollection,您可以在其中添加或删除项目。

    然后在视图(xaml 文件)中将此 ViewModel 添加为 Source 并使用 Binding 将集合中的项目添加到 ListView。

    Here is an example

    如果你不能让它工作,请告诉我。

    【讨论】:

    • 我试图理解这个特性(ObservableCollection),但我猜一切都不正常。 ://
    • 它基本上只是一个列表/集合,但每次您添加或删除内容时,UI 都会自动更新,您无需担心。
    猜你喜欢
    • 2011-02-01
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多