如何在silverlight中与Listbox进行动态绑定?
我正在开发 silverlight 4 应用程序。我正在使用以下列表框进行动态绑定
<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>
我在后面的代码中使用以下代码
List<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; set; }
public Data1()
{
}
public Data1(String a1, String a2)
{
A1 = a1;
A2 = a2;
}
}
我正在使用上述所有代码,但动态绑定不起作用。我的 xaml 或后面的代码有什么问题吗?你能告诉我哪里出错了吗?您能为我提供任何可以解决上述问题的解决方案吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最初的问题是
Data1
类需要是公共的(当前是其内部的)。但是,如果您确实想要动态绑定,那么您可能意味着您希望能够在列表中添加新条目并使它们出现在 UI 中。
因此,您应该使用
ObservableCollection
而不是简单的List
。您可能还希望能够修改各个条目的属性并将这些更改反映在 UI 中,为此,您需要在
Data1
类上实现INotifyPropertyChanged
。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 simpleList<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 yourData1
class.