首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF ItemsControl模板不显示ItemsControl.Items

WPF ItemsControl模板不显示ItemsControl.Items
EN

Stack Overflow用户
提问于 2018-04-13 13:51:29
回答 1查看 403关注 0票数 1

我想从这个xaml代码中创建一个模板或样式,而不需要定义ItemsControl.Items,控件应该是可交换的:

代码语言:javascript
复制
<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>

我试过一种风格:

代码语言:javascript
复制
<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.Items在我的控件所在的原始ItemsControl中,.只是空白而已。当我使用这个ControlTemplate时,也会发生同样的情况:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-13 14:05:32

您在<ItemsPresenter />中缺少了一个ControlTemplate。但是,没有理由仅仅为了能够设置一些属性(如ControlTemplateItemsPanel )而定义自定义的ItemsPanel

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49818608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档