samedi 1 août 2015

wpf binding listbox items to a static list (and another static binding issue)

I really looked hard. There seems to be something I am missing. Using .net 4.5.

#1

Here's my XAML:

<Window x:Class="a.MainWindow"
        xmlns="http://ift.tt/o66D3f"
        xmlns:x="http://ift.tt/mPTqtT"
        xmlns:local="clr-namespace:a"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox x:Name="myListBox" HorizontalAlignment="Left" Height="201" Margin="144,33,0,0" VerticalAlignment="Top" Width="115" ItemsSource="{Binding (local:myClass.myStaticList)}"/>
        <Button Content="Button" HorizontalAlignment="Left" Height="48" Margin="282,247,0,0" VerticalAlignment="Top" Width="150" Click="Button_Click"/>

    </Grid>
</Window>

and here's the code behind:

using System.Collections.ObjectModel;
using System.Windows;

namespace a
{
    public class myClass
    {
        public static ObservableCollection<string> myStaticList { get; set; }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            myListBox.Items.Add("aaa");
            myListBox.Items.Add("bbb");
            myListBox.Items.Add("ccc");
            myListBox.Items.Add("ddd");
            myListBox.Items.Add("eee");
            myListBox.Items.Add("rrr");

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            <--- Breakpoint here
        }
    }
}

Real simple. The button is used only to pause the running with a breakpoint and see whether the data was introduced from myListBox to myControl.myList.

It doesn't. myList remains null. What am I missing?

#2

In addition, on the same subject: I am programmatically constructing an array of checkboxes, and want to bind their isChecked to a similar list - a static propery of a non-static class. I have no idea how to do this.

Help please! TIA




Aucun commentaire:

Enregistrer un commentaire