katit
katit

Reputation: 17915

Items collection must be empty before using ItemsSource in Silverlight

Inside custom control I'm trying to bind my ItemsControl.ItemsSource and get error. Here is how template looks:

<Style TargetType="controls:IdattFilterBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="controls:IdattFilterBox">
                    <ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto">
                        <ItemsControl x:Name="PART_ItemsControl">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Grid HorizontalAlignment="Stretch">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="{Binding Caption}" />
                                        <ComboBox Grid.Column="1">
                                            <ComboBoxItem Content="Contains" />
                                            <ComboBoxItem Content="Begins with" />
                                            <ComboBoxItem Content="Ends with" />
                                        </ComboBox>
                                        <TextBox Grid.Column="2" Text="{Binding FieldFilter1, Mode=TwoWay}" />
                                        <TextBox Grid.Column="3" Text="{Binding FieldFilter2, Mode=TwoWay}" />
                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                                                                                                                            <Border Grid.ColumnSpan="2" x:Name="ValidationErrorElement" BorderBrush="#FFDB000C" BorderThickness="1" CornerRadius="1" Visibility="Collapsed">
                            <ToolTipService.ToolTip>
                                <ToolTip x:Name="validationTooltip" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Template="{StaticResource ValidationToolTipTemplate}">
                                    <ToolTip.Triggers>
                                        <EventTrigger RoutedEvent="Canvas.Loaded">
                                            <BeginStoryboard>
                                                <Storyboard>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible" Storyboard.TargetName="validationTooltip">
                                                        <DiscreteObjectKeyFrame KeyTime="0">
                                                            <DiscreteObjectKeyFrame.Value>
                                                                <System:Boolean>true</System:Boolean>
                                                            </DiscreteObjectKeyFrame.Value>
                                                        </DiscreteObjectKeyFrame>
                                                    </ObjectAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </EventTrigger>
                                    </ToolTip.Triggers>
                                </ToolTip>
                            </ToolTipService.ToolTip>
                            <Grid Background="Transparent" HorizontalAlignment="Right" Height="12" Margin="1,-4,-4,0" VerticalAlignment="Top" Width="12">
                                <Path Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z" Fill="#FFDC000C" Margin="1,3,0,0"/>
                                <Path Data="M 0,0 L2,0 L 8,6 L8,8" Fill="#ffffff" Margin="1,3,0,0"/>
                            </Grid>
                        </Border>
                        </ItemsControl>
                    </ScrollViewer>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>    

In code I' trying to set this PART's ItemSource:

this.WrapperItemsControl.ItemsSource = filterData;

On this line I get error(error message in subject). Why can't I set ItemsSource and what this error means? I want controls in DataTemplate to bind to objects that stored in filterData.

EDIT:

PART_ItemsControl is what my WrapperItemsControl

this.WrapperItemsControl = GetTemplateChild(PartItemsControl) as ItemsControl;

EDIT2:

Screenshot showing that there is one item (Border?) Where does it come from?! enter image description here

EDIT3

DOH! I placed validation border in a wrong spot

Upvotes: 3

Views: 11851

Answers (1)

Joe White
Joe White

Reputation: 97696

You can't use ItemsSource if you manually add items to your ItemsControl. For example, you would get an error if you tried this:

<ItemsControl ItemsSource="{Binding MyItems}">
    <ListBoxItem>Item1</ListBoxItem>
    <ListBoxItem>Item2</ListBoxItem>
</ItemsControl>

You may think you're not doing that, but you actually are. You're adding a single item to your ItemsControl, and that item is of type DataTemplate. Look:

<ItemsControl ...>
    <DataTemplate ... />

That syntax means "create a DataTemplate and add it to the ItemsControl's Items property". (Items is the default property for ItemsControl, so any elements you nest under ItemsControl, if you don't otherwise specify, get added to Items.)

I think you intended to assign that DataTemplate to the ItemsControl's ItemTemplate property, rather than adding it to Items. Try this instead:

<ItemsControl ...>
    <ItemsControl.ItemTemplate>
        <DataTemplate ... />
    </ItemsControl.ItemTemplate>
</ItemsControl>

Upvotes: 14

Related Questions