访问ItemTemplate Silverlight中的模板列表框

本文关键字:列表 ItemTemplate Silverlight 访问 | 更新日期: 2023-09-27 18:10:20

我正在silverlight中创建一个模板控件,它基本上是一个列表框,列表框中的每个项目都有一个组合框。

我试图将组合框选项存储在主控件中作为依赖项属性,然后访问每个项目上的控件,将组合框的ItemsSource绑定到此依赖项属性,但我似乎无法获得正确的绑定,无论是查看单个项目还是主窗口视图模型。以下是我目前拥有的。

public class ReorderList : Control
{
    public ReorderList()
    {
        this.DefaultStyleKey = typeof(ReorderList);
    }
    #region DropDownOptions
    public static readonly DependencyProperty DropDownOptionsProperty = DependencyProperty.Register("DropDownOptions", typeof(IEnumerable<DropDownOption>), typeof(ReorderList), new PropertyMetadata(null));
    public IEnumerable<DropDownOption> DropDownOptions
    {
        get
        {
            return this.GetValue(DropDownOptionsProperty) as IEnumerable<DropDownOption>;
        }
        set
        {
            this.SetValue(DropDownOptionsProperty, value);
        }
    }
    #endregion
    ...
}
模板样式
 <Style TargetType="Controls:ReorderList">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Controls:ReorderList">
                    <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>

                            <Grid Grid.Row="0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition MinWidth="60" Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock x:Name="TitleTextBlock" Grid.Column="0" Text="{Binding Title, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" FontSize="14" Visibility="{Binding TitleVisible, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"  />
                            </Grid>
                            <telerik:RadListBox x:Name="GroupList" Grid.Row="1" AllowDrop="True" ItemContainerStyle="{StaticResource DraggableListBoxItem}" BorderThickness="0" ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource TemplatedParent}}">
                                <telerik:RadListBox.DragDropBehavior>
                                    <Behavior:RestrictedListBoxDragDropBehavior />
                                </telerik:RadListBox.DragDropBehavior>
                                <telerik:RadListBox.DragVisualProvider>
                                    <telerik:ScreenshotDragVisualProvider />
                                </telerik:RadListBox.DragVisualProvider>
                                <telerik:RadListBox.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*" />
                                                <ColumnDefinition Width="110" />
                                            </Grid.ColumnDefinitions>
                                            <TextBlock Grid.Column="0" Text="{Binding Description }"/>
                                            <telerik:RadComboBox Grid.Column="1" SelectedValuePath="Value" DisplayMemberPath="Description" SelectedValue="{Binding SelectedOption, Mode=TwoWay }" ItemsSource="{Binding DataContext.DropDownOptions, ElementName=GroupList}"/>
                                        </Grid>
                                    </DataTemplate>
                                </telerik:RadListBox.ItemTemplate>
                            </telerik:RadListBox>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我已经尝试使用几个不同的选项ItemsSource

ItemsSource="{Binding DataContext.DropDownOptions, ElementName=GroupList}"
ItemsSource="{Binding DataContext.DropDownOptions, RelativeSource={RelativeSource AncestorType=telerik:RadListBox}}"
ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource TemplatedParent}}"

访问ItemTemplate Silverlight中的模板列表框

Ok现在解决了,需要将ListBox的DataContext绑定到模板化的父元素。

DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"

则组合框项源的相对源绑定工作。

ItemsSource="{Binding DataContext.DropDownOptions, RelativeSource={RelativeSource AncestorType=telerik:RadListBox}}"