Click here to Skip to main content
15,747,845 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Ok here several questions


This is my implementation but it is not working atm how can i make it work ?

    <Grid> // xaml part
    <Button Content="Start Crawling Root Sites - This Deletes All Data" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="279" Command="{Binding StartCrawling}" Click="click_start_Crawling"  />
    <ListBox Name="lstBoxEvents" HorizontalAlignment="Left"  Height="138" Margin="294,10,0,0" VerticalAlignment="Top" Width="312">
                <Grid Margin="1">
                        <ColumnDefinition Width="Auto" />
                    <TextBlock Grid.Column="1" Text="{Binding ocEvents}"  />

    private void click_start_Crawling(object sender, RoutedEventArgs e) // inside mainwindow

    public static class PublicStaticFunctions // seperate class
    public static ObservableCollection<string> ocEvents = new ObservableCollection<string>();

    public static void AddMsgToEvents(string srMessage)
        ocEvents.Insert(0, srMessage);

My second question when running multithreaded, if multiple threads access AddMsgToEvents function and adds variable, would listbox still auto refresh without any problem ?

Thank you very much
[no name] 11-Aug-14 20:18pm    
"not working" is not a helpful description of any kind of problem
You do not show where you are setting the binding to the ItemsSource of your ListBox
Collections, AFAIK, are not thread safe.
Sergey Alexandrovich Kryukov 11-Aug-14 23:09pm    
What is not "working"?

1 solution


First issue

You need to bind the ItemsSource of List box then in the textbox each item. It will work

<listbox itemssource="{Binding Components, Mode=OneWay}" /> 

<listbox name="lstBoxEvents" horizontalalignment="Left" height="138" margin="294,10,0,0" verticalalignment="Top" width="312" itemssource="{Binding ocEvents}">
                    <grid margin="1">
                            <columndefinition width="Auto" />
                        <textblock grid.column="1" text="{Binding Path=ocEvent}" />

where ocEvent is a field of type string

for the second issue

if you are using multithreads then there will be issue because a resource locked by one thread cannot be used by other thread. To resolve this you need to use

Application.Current.Dispatcher.Invoke(new Action(delegate
    ocEvents.Insert(0, srMessage);
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900