简体   繁体   中英

How can I use Listbox items with WPF?

I am trying to learn Wpf. When the program runs, it gives me the "no listbox source" error. I am working on a Wpf design but I just started.

The features I have added to the listbox externally, how can I show them as sources. I have no idea about this. I think I have been researching this for 2 hours but I have not found any answer. I have some problems with English. I hope I won't bother you. All details of my codes are below. Thank you in advance for helping.

//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 : I then try to read all in the CronList. I run the method in the class.

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

3 :My Code Dont Run!

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 Code

<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>

I would recomend using a "data binding" in a ViewModel to make the code more readable.

You create a class (eg MainViewModel) and in there you create an ObservableCollection where you add or remove items from.

In the View (xaml file) you then add this ViewModel as a Source and use a Binding to add the items of the collection to your ListView.

Here is an example

Let me know if you can't get it to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM