簡體   English   中英

如何在 WPF 中使用列表框項目?

[英]How can I use Listbox items with WPF?

我正在嘗試學習 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>

我建議在 ViewModel 中使用“數據綁定”以使代碼更具可讀性。

您創建一個類(例如 MainViewModel),然后在其中創建一個ObservableCollection ,您可以從中添加或刪除項目。

在視圖(xaml 文件)中,您然后將此 ViewModel 添加為源並使用綁定將集合的項目添加到您的 ListView。

這是一個例子

如果您無法使用它,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM