8,756
社区成员




<UserControl x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<DataTemplate x:Key="chkBoxTemplate">
<CheckBox Content="{Binding Content, Mode=OneWay}" IsChecked="{Binding Checked, Mode=TwoWay}" />
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Path=Items}">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListBox ItemTemplate="{StaticResource chkBoxTemplate}" ItemsSource="{Binding}" />
<ContentControl Grid.Column="1" ContentTemplate="{StaticResource chkBoxTemplate}" Content="{Binding Path=CurrentItem}" />
</Grid>
</UserControl>
public class MyState : INotifyPropertyChanged
{
private bool? _checked;
public bool? Checked
{
get
{
return _checked;
}
set
{
_checked = value;
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Checked"));
}
}
public string Content { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += (s, e) =>
{
this.DataContext = this;
};
}
private List<MyState> _items;
public ICollectionView Items
{
get
{
if (_items == null)
{
_items = new List<MyState>();
for (int i = 0; i < 40; i++)
_items.Add(new MyState { Checked = false, Content = i.ToString() });
}
// 引用System.Windows.Data.dll
return new PagedCollectionView(_items);
}
}
}