ItemsControl面板生成

时间:2015-09-18 16:02:00

标签: wpf

有人知道我如何确定是否已生成itemscontrol面板?

我正在构建一个包含itemscontrol的自定义控件,并将Grid设置为ites面板,并且无法找到从后面的代码中获取itemscontrol面板的正确方法。

控制模板

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

那么获得实际Panel的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以简单地使用其Loaded事件并在那里访问它。类似的东西:

XAML:

<Grid>
    <ItemsControl Loaded="OnItemsControlLoaded">

    </ItemsControl>
</Grid>

代码隐藏:

    private void OnItemsControlLoaded(object sender, RoutedEventArgs e)
    {
        var itemsControl = sender as ItemsControl;
        if (itemsControl != null)
        {
            // use here
        }
    }