wpf - WrapPanel 作为 ItemsControl 的 ItemPanel

标签 wpf styles itemscontrol itemspanel itemspresenter

仍然在玩 WPF 并边学习边学习。现在尝试构建一个动态的控件分组(主要是按钮,但可能包括复选框等)。

我不知道这样做的最佳方法是什么,所以我尝试创建一个 ItemsControl 样式,然后将项目添加到 WrapPanel 内的 ItemsPresenter 中。很快意识到这些项目不会包装,因为它们实际上不在 WrapPanel 内,除非我将其作为 ItemsHost。像这样:

<Style x:Key="ButtonPanelGroup" TargetType="{x:Type ItemsControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ItemsControl}">
                <Border CornerRadius="5"
                        BorderBrush="{StaticResource DarkColorBrush}"
                        BorderThickness="1"
                        Margin="5">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

                        <WrapPanel IsItemsHost="True" FlowDirection="LeftToRight">
                            <ItemsPresenter />
                        </WrapPanel>

                        <ContentPresenter ContentSource="Name" Grid.Row="1" />

                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

请注意,这是一项正在进行的工作,我仍然需要实现许多样式效果。我在这里使用它:
<UniformGrid Rows="1">
    <ItemsControl Name="Group1" Style="{StaticResource ButtonPanelGroup}">
        <Button>Button1</Button>
        <Button>Button2</Button>
        <CheckBox>TickBox</CheckBox>
    </ItemsControl>

    <ItemsControl Name="Group2" Style="{StaticResource ButtonPanelGroup}">
        <Button>Button3</Button>
        <Button>Button4</Button>
        <Button>Button5</Button>
    </ItemsControl>

    <ItemsControl Name="Group3" Style="{StaticResource ButtonPanelGroup}">
        <Button>Button6</Button>
        <Button>Button7</Button>
        <Button>Button8</Button>
    </ItemsControl>
</UniformGrid>

还要注意,它仍然是一项正在进行的工作,因为 UniformGrid 不会成为这里的方法,而且边距也可能是一个痛苦(是否有任何边距重叠?)所以输入将不胜感激。

现在到了真正的问题。这不起作用我收到一个错误:

'ItemsPresenter' object cannot be added to 'WrapPanel'. Cannot explicitly modify Children collection of Panel used as ItemsPanel for ItemsControl. ItemsControl generates child elements for Panel. Error at object 'System.Windows.Controls.ItemsPresenter'.



那么做这样的事情的最佳方法是什么(希望能够将按钮和其他控件放入 ItemControl 并且排列非常好)。将控件放入某种集合并进行迭代会更好。

很想把它做得很好,但我的 WPF 技能仍然缺乏。是否有任何 WPF 书籍可以教授基础知识并展示专业人士如何真正做到这一点?

最佳答案

您可能想看看 ItemsPanel属性(property):

Gets or sets the template that defines the panel that controls the layout of items.



例子:
<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

您可以将其设置为样式,如下所示:
<Style TargetType="ItemsControl">
    <Setter Property="ItemsPanel">
      <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

关于wpf - WrapPanel 作为 ItemsControl 的 ItemPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3131919/

相关文章:

c# - 查找事件文本框 WPF

c# - 在集合中公开的模型中实现 INotifyPropertyChanged

jQuery to FadeOut 由 HTML 内联样式声明的背景图像

wpf - 如何绑定(bind)到祖先数据上下文中的属性?

c# - 如何更改整个 Canvas 的鼠标光标形状?

android - 更改水平进度条颜色

styles - Material-UI 主题 : How to manage order or priority the . MuiXXX 类是否应用?

WPF:构建我自己的分页项目列表....;)?

.net - 命名为 RowDefinition 的网格无法作为 WPF4 中的 ItemsPanel 填充

wpf - 在 WPF 中使用多个修饰键创建 KeyBinding