在ItemTemplate Silverlight中访问模板化列表框

时间:2014-06-25 16:33:00

标签: c# xaml templates mvvm silverlight-5.0

我在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}}"

1 个答案:

答案 0 :(得分:0)

确定现在已经完成了,需要将ListBox的DataContext绑定到模板化的父级。

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

然后组合框项目源的相对源绑定工作。

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