WPF ItemsControl模板不显示ItemsControl.Items

时间:2018-04-13 13:51:30

标签: c# wpf templates styles

我想从这个xaml代码创建一个模板或一个Style而不定义ItemsControl.Items,控件应该可以交换:

<ItemsControl Style="{DynamicResource ItemsControlWithMargin1}" Height="50">
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="Control">
            <Setter Property="Margin" Value="15 0 0 0"></Setter>
        </Style>
    </ItemsControl.ItemContainerStyle>

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.Items>

        <Image Style="{DynamicResource WarningImage}"/>
        <Label Style="{DynamicResource InfoLabel1}">

    </ItemsControl.Items>

</ItemsControl>

我尝试过Style:

<Style x:Key="ItemsControlWithMargin1" TargetType="ItemsControl">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ItemsControl">

                            <ItemsControl>
                                <ItemsControl.ItemContainerStyle>
                                    <Style TargetType="Control">
                                        <Setter Property="Margin" Value="15 0 0 0"></Setter>
                                    </Style>
                                </ItemsControl.ItemContainerStyle>
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal"/>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            </ItemsControl>

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

但是当我设置样式时,我没有在我的控件所在的原始ItemsControl中看到我的ItemsControl.Items,它只是空白。使用此ControlTemplate时会发生同样的情况:

<ControlTemplate x:Key="ItemsControlWithMargin2" TargetType="{x:Type ItemsControl}">

        <ItemsControl>
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="Control">
                    <Setter Property="Margin" Value="15 0 0 0"></Setter>
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>

</ControlTemplate>

1 个答案:

答案 0 :(得分:0)

您在<ItemsPresenter />中遗漏了ControlTemplate。但是,没有理由定义自定义ControlTemplate只是为了能够设置一些属性,如MarginItemsPanel

<Style x:Key="ItemsControlWithMargin1" TargetType="ItemsControl">
    <Setter Property="Margin" Value="15 0 0 0"></Setter>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>