David Molyneux
David Molyneux

Reputation: 304

Silverlight Template Control Set ItemTemplate

I'm building a control in Silverlight that contains a listbox, At the moment I have a list box and a title text. What I'm trying to do is expose/set the item template of the list box so when I add the new control to my usercontrol I can use the following.

I have tried setting a DataTemplate as a Dependency Property but nothing got set. I know at the moment it looks rather simple but I want to expand on this but need this basic element before trying to expand the control.

Thanks

David.

Intended usage in xaml:

<UserControls:NewListControl>
    <UserControls:NewListControl.Items>
        <UserControls:MultiSelectItem StringCode="100" Description="GS" />
        <UserControls:MultiSelectItem StringCode="200" Description="GC" />
        <UserControls:MultiSelectItem StringCode="200" Description="GC" />
        <UserControls:MultiSelectItem StringCode="200" Description="GC" />
        <UserControls:MultiSelectItem StringCode="200" Description="GC" />
    </UserControls:NewListControl.Items>
    <UserControls:NewListControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <TextBox Text="{Binding StringCode}" />
            </Grid>
        </DataTemplate>
</UserControls:NewListControl.ItemTemplate>
</UserControls:NewListControl>

The control's code:

public class NewListControl : Control
{

 public NewListControl()
 {
     this.DefaultStyleKey = typeof(NewListControl);
 }

    public DataTemplate ItemTemplate
    {
        get
        {
            return (DataTemplate)GetValue(ItemTemplateProperty);
        }
        set
        {
            SetValue(ItemTemplateProperty, value);
        }
    }

    public static readonly DependencyProperty ItemTemplateProperty =
        DependencyProperty.Register("ItemTemplate",
            typeof(DataTemplate), typeof(NewListControl), null);

}

The control's default style and template:

<Style TargetType="local:NewListControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:NewListControl">
                <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <TextBlock
                            Name="TitleTextBlock"
                            Grid.Row="0" FontSize="20"
                            Text="{Binding Title, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                            Visibility="{Binding TitleVisible, ElementName=userControl}"/>
                        <telerik:RadListBox
                            x:Name="GroupList"
                            Grid.Row="1" AllowDrop="True"
                            ItemContainerStyle="{StaticResource DraggableListBoxItem}"
                            ItemTemplate="{TemplateBinding ItemTemplate}"
                            BorderThickness="0"
                            ItemsSource="{Binding Items, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
                            <telerik:RadListBox.DragDropBehavior>
                                <Behavior:RestrictedListBoxDragDropBehavior />
                            </telerik:RadListBox.DragDropBehavior>
                            <telerik:RadListBox.DragVisualProvider>
                                <telerik:ScreenshotDragVisualProvider />
                            </telerik:RadListBox.DragVisualProvider>
                        </telerik:RadListBox>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Upvotes: 1

Views: 393

Answers (1)

David Molyneux
David Molyneux

Reputation: 304

This turned out to be the ItemContainerStyle causing the issue.

I have a style that allows the items in the list to be dragable:

Originally I had the style set like this:

<Style x:Key="DraggableListBoxItem" 
       TargetType="telerik:RadListBoxItem" >
    <Setter Property="telerik:DragDropManager.AllowCapturedDrag" Value="True" />
</Style>

As I am using the implicit styles for Telerik Controls, I had missed one key bit BasedOn. with out this the style would never use the Item Template.

<Style x:Key="DraggableListBoxItem" 
       TargetType="telerik:RadListBoxItem" 
       BasedOn="{StaticResource RadListBoxItemStyle}">
    <Setter Property="telerik:DragDropManager.AllowCapturedDrag" Value="True" />
</Style>

Thanks Martin for the help what you suggested was correct just another part of the code blocking it.

Upvotes: 1

Related Questions