【问题标题】:ItemContainerStyle for Canvas-based ItemsControl基于 Canvas 的 ItemsControl 的 ItemContainerStyle
【发布时间】:2013-08-27 10:05:53
【问题描述】:

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

<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 为所有项目(带有按钮等)设置一个公共边框镶边。

Canvas 为每个绑定项目创建一个 ContentPresenter。正如您在上面看到的,我曾希望能够为 ItemContainerStyle 中的 ContentPresenter 指定一个 ContentTemplate,但这不起作用,因为我假设它本质上创建了一个循环引用。

提前致谢!

【问题讨论】:

  • 你应该为你的 ItemControl 定义一个 Style ,在那里你可以通过 Triggers 动态设置 ItemTemplate 和 ItemContainerStyle 。

标签: wpf wpf-controls datatemplate itemscontrol


【解决方案1】:

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

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

编辑:也许你必须写

<ContentPresenter Margin="2"
                  Content="{TemplateBinding Content}"
                  ContentTemplate="{TemplateBinding ContentTemplate}"/>

【讨论】:

  • 似乎合乎逻辑,但这只是呈现一个 ListBoxItem,然后是黄色网格和每个项目的空 ContentPresenter。它不再拾取 DataTemplate。
  • 在我的测试程序中确实如此。您确定这些项目是ColouredWidget 对象吗?
猜你喜欢
  • 1970-01-01
  • 2012-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
相关资源
最近更新 更多