0

We're trying to make a custom control that contains a wrapped section of text boxes for each item in the list bound to it.

Like this:

<ItemsControl x:Name="asdf">
 <ItemsControl.ItemsPanel>
  <ItemsPanelTemplate>
   <controls:WrapPanel />
  </ItemsPanelTemplate>
 </ItemsControl.ItemsPanel>
 <ItemsControl.ItemTemplate>
  <DataTemplate>
   <TextBox Text="{Binding}" />
  </DataTemplate>
 </ItemsControl.ItemTemplate>
</ItemsControl>

However, when we turn this into a custom control, it doesn't set the ItemsPanel to a WrapPanel, nor does it do the ItemTemplate either:

<ItemsControl x:Class="SilverlightApplication1.PillTagBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls= "clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit">

 <ItemsControl.ItemsPanel>
  <ItemsPanelTemplate>
   <controls:WrapPanel />
  </ItemsPanelTemplate>
 </ItemsControl.ItemsPanel>
 <ItemsControl.ItemTemplate>
  <DataTemplate>
   <TextBox Text="{Binding}" />
  </DataTemplate>
 </ItemsControl.ItemTemplate>

</ItemsControl>

It just shows the bound list of items like there was no styling at all:

<ItemsControl x:Name="asdf" />

How do we make the first chunk of XAML into a custom control?

Thanks!

1 Answer 1

2

For what you want to do, you don't need a custom control, a style is enough:

<Style x:Key="MyControlStyle" TargetType="ItemsControl">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <controls:WrapPanel/>
            </ItemsPanelTemplate>
        </Setter.Value>  
    </Setter>
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBox Text="{Binding}" />
            </DataTemplate>
        </Setter.Value>  
    </Setter>
</Style>

And then at the instance:

<ItemsControl x:Name="asdf" Style="{StaticResource MyControlStyle}" />

If you do need a custom control for other reasons:

  1. Create a new project with the Silverlight Control Library template (all definitions are in this project).
  2. If there is no Themes folder, add it to the root of the project and create a new ResourceDictionary file named Generic.xaml in the Themes folder.
  3. Create a new class, inherit from ItemsControl (lets call it MyItemsControl).
  4. Add a constructor like this:

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

  5. Add the style above to the Generic.xaml file, remove the x:Key attribute and change the TargetType to MyItemsControl (you'll need to add a xmlns definition for the local namespace).
  6. Now go back to your client project, make a reference to the Control Library project.
  7. Add xmlns definition in the appropriate Page\UserControl xaml file and use the MyItemsControl as any other ItemsControl.
Sign up to request clarification or add additional context in comments.

2 Comments

Well, this was a simplistic example for demonstration reasons. I want to override OnItemsSourceChanged and have various click events self contained in one class I can use throughout my project.
I thought that was the case. Follow the listed steps and, if you need special items, don't forget to override IsItemItsOwnContainerOverride and GetContainerForItemOverride methods - gets me every time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.