开发者

How to do dynamic binding with Listbox in silverlight?

I am developing silverlight 4 application. I am using the following listbox for dynamic binding

<ListBox Margin="44,100,46,138" x:Name="lstbox1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding A1}" Foreground="Gray" FontSize="14" Width="100" Height="20" ></TextBlock>
                        <TextBlock Text="{Binding A2}" Foreground="Red" Width="100" Height="20" ></TextBlock>
                        <Line X1="-3400" Y1="32" X2="10" Y2="32" Stroke="Gray" StrokeThickness="1"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

I am using the following code in code behind

List<Data1> ObserCollObj = new List<Data1>();
        public MainPage()
        {
            InitializeComponent();
            Data1 obj1 = new Data1("aaa", "dasd");
            ObserCollObj.Add(obj1);
            lstbox1.ItemsSource = ObserCollObj;

        }

I am using the following class

class Data1
    {
        public String A1 { get; set;}
        public String A2 { get; set; }
        public Data1()
        {
        }
        public Data1(String a1, String a2)
        {
            A1 = a1;
            A2 = a2;
        }
    }

I am using all above code but the dynamic binding does not working.Is anything wrong in my xaml or code behind ? Can you please tell me where I am going wrong ? Can you plea开发者_运维问答se provide me any solution through which I can resolve the above issue?


The initial problem is that the Data1 class needs to be public (currently its internal).

However if you really want dynamic binding then you probably mean that you want to be able to add new entries on the list and for them to appear in the UI.

You should therefore use a ObservableCollection<Data1> instead of a simple List<Data1>.

You may also want to be able to modify the properties of individual entries and have those changes reflected in the UI, to do that you need to implement INotifyPropertyChanged on your Data1 class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜