如何用Silverlight中的Listbox进行动态绑定? [英] How to do dynamic binding with Listbox in silverlight?

查看:85
本文介绍了如何用Silverlight中的Listbox进行动态绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发silverlight 4应用程序。我使用以下列表框进行动态绑定

 < ListBox Margin =44,100,46,138x:Name =lstbox1 > 
< ListBox.ItemTemplate>
< DataTemplate>
< StackPanel Orientation =Vertical>
< TextBlock Text ={Binding A1}Foreground =GrayFontSize =14Width =100Height =20>< / TextBlock>
< TextBlock Text ={Binding A2}Foreground =RedWidth =100Height =20>< / TextBlock>
<线X1 = - 3400Y1 =32X2 =10Y2 =32笔画=灰色笔画厚度=1/>
< / StackPanel>
< / DataTemplate>
< /ListBox.ItemTemplate>
< / ListBox>

我在



中的代码中使用以下代码

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

}

我正在使用以下课程

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

我使用所有上述代码,但动态绑定不工作。我的xaml或代码背后有什么问题吗?你能告诉我我哪里错了吗?你可以提供我解决上述问题的解决方案吗?

解决方案

最初的问题是 Data1 类需要公开(目前为内部)。



但是,如果你真的想要动态绑定,那么你可能意味着你希望能够在列表中添加新条目,并将它们显示在UI中。



因此,您应该使用 ObservableCollection< Data1> 而不是简单的列表< Data1>



您可能还想能够修改个人条目的属性,并在UI中反映出这些更改,您需要在 Data1 INotifyPropertyChanged c $ c> class。


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 please 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.

这篇关于如何用Silverlight中的Listbox进行动态绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆