简体   繁体   English

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

[英]Access Templated Listbox in ItemTemplate Silverlight

I am creating a templated control in silverlight that is basically a list box, and each item in the list box will have a combo box on it. 我正在Silverlight中创建一个模板控件,该控件基本上是一个列表框,并且列表框中的每个项目都将具有一个组合框。

I am trying to store the combobox options in the main control as a dependency property and then access the contol on each item to bind the ItemsSource of the combobox to this dependency property but I cant seem to get the binding correct, either its looking at the individual item or the main window view model. 我试图将组合框选项存储在主控件中作为依赖项属性,然后访问每个项目上的控制以将组合框的ItemsSource绑定到此依赖项属性,但是我似乎无法正确地进行绑定,无论是查看单个项目或主窗口视图模型。 Below is what i currently have. 以下是我目前拥有的。

The Control 控制

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

    ...
}

The Template Style 模板样式

 <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>

I have tried using a few different option for the ItemsSource 我尝试为ItemsSource使用一些其他选项

ItemsSource="{Binding DataContext.DropDownOptions, ElementName=GroupList}"

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

ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource TemplatedParent}}"

Ok Worked it out now, needed to bind the DataContext of the ListBox to the templated parent. 确定现在就解决了,需要将ListBox的DataContext绑定到模板化父对象。

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

Then the relative source binding for the combobox items source works. 然后,组合框项目源的相对源绑定起作用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM