自定义控件中的依赖属性内部绑定

时间:2013-08-22 15:32:30

标签: c# wpf xaml user-controls

我正在尝试将UserControl(一组radiobuttons)中包含的ItemsControl的ItemsSource属性绑定到同一UserControl的属性。当使用UserControl时,这应该由DependencyProperty驱动。但是我无法使其工作,如果相反我硬编码控件名称,分配正常工作。

这是班级:

public partial class GroupListBox : UserControl
{
    public GroupListBox()
    {
        InitializeComponent();
    }

    private List<RadioButton> _itemsList = new List<RadioButton>();
    public List<RadioButton> ItemsList
    {
        get { return _itemsList; }
        set
        {
            _itemsList = value;
        }
    }

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

    public static readonly DependencyProperty ItemsSourceProperty = 
            DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(GroupListBox), new FrameworkPropertyMetadata(null, OnItemsSourceChanged));

    private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = (GroupListBox)d;
        var grpName = control.GetHashCode().ToString(); // used to generate a unique GroupName
        var oldValue = (IEnumerable)e.OldValue;
        var newValue = (IEnumerable)e.NewValue;

        if ((e.NewValue == null) && !BindingOperations.IsDataBound(d, ItemsSourceProperty))
        {
            control.ItemsList.Clear();
        }
        else
        {
            var radioItems = (from object item in newValue select new RadioButton() { Content = item, GroupName = grpName }).ToList();
            //control.container.ItemsSource = radioItems; // *** this works
            control.ItemsList = radioItems; // *** this doesn`t
        }
    }

这是XAML

<Border x:Name="brdMain">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock x:Name="txtTitle" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="0" Text="" />
        <Image x:Name="imgImage" Grid.Column="0" Grid.Row="1" />
        <ScrollViewer x:Name="scroll" Grid.Row="1" Grid.Column="1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"  CanContentScroll="True">
            <!--This is the control I want to bind-->
            <ItemsControl x:Name="container"  ItemsSource="{Binding Path=ItemsList}" >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </ScrollViewer>
    </Grid>
</Border>

像这样使用:

<cust:GroupListBox ItemsSource="{Binding SymmetryAxis}"></cust:GroupListBox>

我确定我的控制绑定有问题,但我不知道是什么。 任何线索?

编辑: 我在XAML中做了一些小改动: 现在它看起来像这样:

<ItemsControl Name="container" Width="{Binding ElementName=scroll, Path=ViewportWidth}" ItemsSource="{Binding Path=ItemsSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:GroupListBox}}}" ItemTemplate="{StaticResource ItemPanelDatatemplate}" />

这是DataTemplate:

<DataTemplate DataType="{x:Type ItemsPanelTemplate}" x:Key="ItemPanelDatatemplate">
    <RadioButton Content="{Binding}" GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl}, Path=Name}"></RadioButton>    </DataTemplate>

这无论如何都会给GroupName带来问题,因为它总是空的,然后所有的RadioButton就像它们在一个组中一样。

如何在后面的代码中获得相同的结果(例如为每个组分配一个唯一的名称?

0 个答案:

没有答案