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

First

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">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Margin="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="1" Text="{Binding ocEvents}"  />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>



    private void click_start_Crawling(object sender, RoutedEventArgs e) // inside mainwindow
{
    PublicStaticFunctions.AddMsgToEvents("trial");
}


    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
Posted
Comments
[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"?
—SA

1 solution

Hi,

First issue

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

e.g:-
XML
<listbox itemssource="{Binding Components, Mode=OneWay}" /> 


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


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

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