0

我正在尝试将我的 UserControl(一组单选按钮)中包含的 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 带来问题,因为它总是空的,然后所有 RadioButtons 的行为就像它们在一个组中一样。

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

4

0 回答 0