ChecklightItems属性,用于Silverlight中的自定义CheckBoxList控件

时间:2009-12-14 12:54:16

标签: silverlight data-binding

我需要使用ItemsSource和CheckedItems属性实现CheckBoxList控件。如果CheckedItems包含这些值,则ItemsSource中的项目应显示为选中的复选框,否则应取消选中。另外,我需要对CheckedItems属性的双向数据绑定支持(当用户点击复选框时,应该更新此属性的值)。

这里有一些代码可能有助于理解我的问题

XAML:

<UserControl x:Class="Namespace.Controls.CheckBoxList" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ListBox x:Name="LayoutRoot">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</UserControl>

代码背后:

public partial class CheckBoxList : UserControl
{
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(CheckBoxList), null);
    public static readonly DependencyProperty CheckedItemsProperty = DependencyProperty.Register("CheckedItems", typeof(IEnumerable), typeof(CheckBoxList), null);

    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }
    public IEnumerable CheckedItems
    {
        get { return (IEnumerable)GetValue(CheckedItemsProperty); }
        set { SetValue(CheckedItemsProperty, value); }
    }

    public CheckBoxList()
    {
        InitializeComponent();
        LayoutRoot.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("ItemsSource") { Source = this });
    }
}

我认为我需要使用自定义转换器将ListBox绑定到UserControl,该转换器将返回具有其他IsChecked属性的项集合,但仅在单向数据绑定的情况下才有效。

看起来我需要同时对两个属性进行双向绑定,但我不知道如何实现它,并会对此问题有任何帮助。

提前致谢。

2 个答案:

答案 0 :(得分:3)

首先,您应该考虑从ListBox而不是UserControl派生。 ListBox已经完成了你想要的大部分工作。

其次考虑一种方式绑定到IList。然后,您可以在选择相应项目时向该IList添加和删除entires。

不是尝试在项模板中绑定CheckBox控件而是复制ListBox样式,而是将它们作为新控件的样式放在Generic.xaml中。然后使用选中和未选中的复选框修改未选择和选定的视觉状态,作为视觉外观的一部分。

现在您可以附加到SelectionChanged事件并使用Event args AddedItems列表添加到绑定的IList和RemovedItems列表以从绑定列表中删除项目。

当您分配CheckedItems或更改ItemsSource时,您需要清除并重新添加项目集到SelectedItems列表框列表。

可能有一些问题需要解决,但这似乎是一个更直接的目标,而不是从头开始使用UserControl基础。

答案 1 :(得分:0)

将列表框数据源的可观察集合添加到datacontext:

private ObservableCollection<MyItem> _myItems;
public ObservableCollection<MyItem> MyItems
{
    get { return _searchByFields; }
    set
    {
        _myItems = value;
    }
}

添加一个类来保存有关复选框的数据:

public class MyItem
{
  public bool Checked {get; set; }
  public string MyItemValue { set ; set; }
}

然后在您的数据模板中将列表框绑定到集合,并将数据模板复选框绑定到相应的MyItem属性:

<UserControl x:Class="Namespace.Controls.CheckBoxList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">    
    <ListBox x:Name="LayoutRoot"
             DataContext="[Dataconext here]"
             ItemsSource={Binding MyItems}>        
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding Checked, Mode=TwoWay}" 
                          Content="{Binding MyItemValue}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</UserControl>

不要忘记将绑定的DataContext设置为适当的类(您可能在XAML或后面的代码中执行此操作)