views:

29

answers:

2

My listbox is not displaying the items. The items are in a List<string>.

Here is the xaml:

<DockPanel Margin="10,10,10,10">
    <DockPanel.Resources>
        <local:MyErrors x:Key="myErrors"/>
    </DockPanel.Resources>
    <ListBox DockPanel.Dock="Top" ItemsSource="{StaticResource myErrors}" Height="300" Width="250" Margin="0,5,0,10" 
        />

    <StackPanel DockPanel.Dock="Bottom" HorizontalAlignment="Center" VerticalAlignment="Bottom" Orientation="Horizontal">
        <Button Height="28" Click="buttonOK_Click" Margin="10,10,10,10" IsDefault="True" Name="buttonOK" Width="75">OK</Button>
        <Button Height="28" Click="buttonCancel_Click" Margin="10,10,10,10" IsCancel="True" Name="buttonCancel" Width="75">Cancel</Button>
    </StackPanel>

</DockPanel>

I am setting the source like this:

DialogErrors dlg = new DialogErrors();
                    dlg.Owner = App.Current.MainWindow as Window;
                    dlg.MyErrors = myOtherClass.MyErrors;

Then I have an automatic property in the dialog. Which is the List<string> derived class whose type and name are MyErrors.

        public MyErrors MyErrors
    {
        get;
        set;
    }

enter code here
A: 

You should look at using an ObservableCollection to store your error strings.

Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

This implements the INotifyPropertyChanged and INotifyCollectionChanged interfaces which notify clients - in this case the UI - that the property and collection have changed.

It might seem a bit over the top if they don't change during the life of the program, but it does all the wiring you need to get the UI to update initially.

ChrisF
I just wish to display a read-only list of strings that are errors. They will not get added to by any other source or function while being displayed.
tim
@tim - you could use the `INotifyPropertyChanged` interface directly then. Follow the link for example usage. However, you would only be implementing what `ObservableCollection` already does.
ChrisF
I tried changing the property of the dialog to be observable collection and changed the assignment to the following, yet still nothing displayed.dlg.MyErrors = new System.Collections.ObjectModel.ObservableCollection<string>(myOtherClass.MyErrors);
tim
+1  A: 

Your resources are pointing to a new MyErrors and not the one defined in the dialog.

Just get rid of the resources section and bind directly to MyErrors:

<ListBox DockPanel.Dock="Top" ItemsSource="{Binding MyErrors}" Height="300" Width="250" Margin="0,5,0,10" />
cristobalito
Tried that an still does not work. Is there something I can try to troubleshoot. Does not the itemsource automatically default to ToString on the item?
tim
You need to also specify a source for the Binding. By default it uses the inherited DataContext, which I assume from your code sample is probably null. Use this Binding instead to explicitly set the source to the control itself: {Binding Path=MyErrors, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:DialogErrors}}}
John Bowen