【发布时间】:2010-11-26 16:29:56
【问题描述】:
我有一个列表框。在每个 ListBoxItem 中有两个 TextBlock 和一个 CheckBox。起初我检查了所有复选框。取消选中一个复选框然后滚动后,复选框会随机选中和取消选中。
怎么了?
代码:
cut from [X.xaml.cs]
//
lbx.ItemsSource = settings.itemSettings; // settings is instance of Settings
// holds data for the listbox
public class Settings
{
public ObservableCollection<ItemSetting> itemSettings; // <-lbx.ItemSource
public Settings(ObservableCollection<string> texts)
{
itemSettings = new ObservableCollection<ItemSetting>();
for (int i = 0; i < texts.Count; i++)
{
ItemSetting s = new ItemSetting();
s.number = (i + 1).ToString(); // the position number
s.text = texts[i]; // the message
s.show = true; // true=show or false=hide
itemSettings.Add(s);
}
}
}
// holds data for listbox item
public class ItemSetting
{
public string number { get; set; }
public string text { get; set; }
public bool show { get; set; }
}
------------------------------------------------------------------------------
cut from [X.xaml] --- the listbox
<ListBox Name="lbx">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding number}" />
<TextBlock Text="{Binding text}" />
<CheckBox IsChecked="{Binding show}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
【问题讨论】:
标签: checkbox listbox silverlight-4.0