0

Does anybody know how i can determine if itemscontrol panel has been generated?

I am building a custom control that contains itemscontrol with Grid set as ites panel and cant figure out the proper way to obtain the itemscontrol panel from code behind.

Control Template

    <Style TargetType="{x:Type local:DayTimeRange}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:DayTimeRange}">

                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">

                    <ItemsControl 
                                      Name="PART_ItemsControl" 
                                      AlternationCount="1"
                                      IsEnabled="{Binding HasTimeRange,RelativeSource={RelativeSource AncestorType=local:DayTimeRange}}">
                        <ItemsControl.ItemContainerStyle>
                            <Style>
                                <Setter Property="Grid.Row" Value="{Binding GridRow,Mode=OneWay}" />
                                <Setter Property="Grid.Column" Value="{Binding GridColumn,Mode=OneWay}" />
                                <Setter Property="Grid.RowSpan" Value="{Binding GridRowSpan,Mode=OneWay}" />
                                <Setter Property="Grid.ColumnSpan" Value="{Binding GridColumnSpan,Mode=OneWay}" />
                            </Style>
                        </ItemsControl.ItemContainerStyle>
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <Grid x:Name="PART_Panel" IsItemsHost="True"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>


                </Border>



            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

So what would be the best way to get the actual Panel?

NullReference
  • 862
  • 1
  • 11
  • 27

1 Answers1

0

You can simply used its Loaded event and access it there. Something like:

xaml:

<Grid>
    <ItemsControl Loaded="OnItemsControlLoaded">

    </ItemsControl>
</Grid>

code-behind:

    private void OnItemsControlLoaded(object sender, RoutedEventArgs e)
    {
        var itemsControl = sender as ItemsControl;
        if (itemsControl != null)
        {
            // use here
        }
    }
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • I get this when i try to attach the event handler Severity Code Description Project File Line Error 'ResourceDictionary' root element requires a x:Class attribute to support event handlers in the XAML file. Either remove the event handler for the Loaded event, or add a x:Class attribute to the root element. – NullReference Sep 18 '15 at 16:59
  • post your entire XAML, i need to see how you are using the ItemsControl. It sounds like it is not actually within a class – d.moncada Sep 18 '15 at 17:01
  • I am not deriving it if you mean that. The itemscontrol is part of overall custom control template. – NullReference Sep 18 '15 at 17:50