tim
tim

Reputation: 1369

Binding List to Listbox

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

Upvotes: 0

Views: 618

Answers (2)

cristobalito
cristobalito

Reputation: 4272

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

Upvotes: 1

ChrisF
ChrisF

Reputation: 137108

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.

Upvotes: 0

Related Questions