首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于画布的ItemContainerStyle ItemsControl

基于画布的ItemContainerStyle ItemsControl
EN

Stack Overflow用户
提问于 2013-08-27 10:05:53
回答 1查看 3.2K关注 0票数 2

我有以下ItemsControl,我使用画布作为面板:

代码语言:javascript
复制
<ItemsControl ItemsSource="{Binding Widgets}">
    <ItemsControl.Resources>
        <DataTemplate DataType="{x:Type widgetLayoutSpike:ColouredWidget}">
            <Grid Background="{Binding BgColour}">
                <TextBlock Text="{Binding Title}" />
            </Grid>
        </DataTemplate>
    </ItemsControl.Resources>

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Grid Background="Yellow">
                            <!--  <ContentPresenter />  -->
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

我的要求是:

  • ItemTemplates是通过类型化的DataTemplates设置的(上面为ColouredWidget项类型显示了一个示例)。对于不同的项目类型,其中可能有几个。
  • 我需要能够指定一个ItemContainer模板,它封装了所有不同的ItemTemplates。具体的用例是,我希望这个ItemContainer为所有项目(带有按钮等)设置一个公共边框。

画布为每个绑定项创建一个ContentPresenter。正如您在上面看到的,我本来希望能够在ContentTemplate中为ContentPresenter指定一个ItemContainerStyle,但这是行不通的,因为我假设它实际上创建了一个循环引用。

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-27 11:07:59

使用ListBox而不是ItemsControl可能更容易做到这一点,因为容器类型是ListBoxItem,而ListBoxItem(与ContentPresenter相反)有一个控制模板,您可以在样式中替换该模板:

代码语言:javascript
复制
<ListBox ItemsSource="{Binding Widgets}">
    <ListBox.Resources>
        <DataTemplate DataType="{x:Type widgetLayoutSpike:ColouredWidget}">
            <Grid Background="{Binding BgColour}">
                <TextBlock Text="{Binding Title}" />
            </Grid>
        </DataTemplate>
    </ListBox.Resources>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid Background="Yellow">
                            <ContentPresenter Margin="2"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

编辑:也许你必须写

代码语言:javascript
复制
<ContentPresenter Margin="2"
                  Content="{TemplateBinding Content}"
                  ContentTemplate="{TemplateBinding ContentTemplate}"/>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18462512

复制
相关文章

相似问题

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