WrapPanel作为ItemsControl的ItemPanel

Ing*_*als 31 wpf styles itemscontrol itemspanel itemspresenter

仍在和WPF一起玩弄,并在我学习的同时学习.现在尝试构建一个动态的控件分组(主要是按钮,但可能包括CheckBoxes和其他).

我不知道最好的方法是什么,所以我尝试创建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>
Run Code Online (Sandbox Code Playgroud)

请注意,这是一项正在进行的工作,我仍然需要实现许多样式效果.我在这里用它:

<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>
Run Code Online (Sandbox Code Playgroud)

另请注意,它仍然是一项正在进行中的工作,因为UniformGrid不会成为这里的方式,并且边距可能是一种痛苦(是否有任何重叠的边距?)所以输入会受到赞赏.

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

'ItemsPresenter'对象无法添加到'WrapPanel'.无法显式修改用作ItemsControl的ItemsPanel的Panel的Children集合.ItemsControl为Panel生成子元素.对象'System.Windows.Controls.ItemsPresenter'出错.

那么做这样的事情的最佳方法是什么(希望能够将按钮和其他控件扔进ItemControl并且排队真的很棒).将控件放入某种集合并迭代会更好吗?

我希望能很好地完成它,但我的WPF技能仍然缺乏.是否有任何WPF书籍超出了基础知识,并展示专业人士将如何真正做到这一点?

Arc*_*rus 49

您可能想要查看ItemsPanel属性:

获取或设置用于定义控制项布局的面板的模板.

例:

<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

你可以按如下方式设置它:

<Style TargetType="ItemsControl">
    <Setter Property="ItemsPanel">
      <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)


Mr.*_*r.B 5

不要忘记线索属性 IsItemsHost="True" 的定义。否则您的 ItemsControl 将不会显示您的项目。

<ListBox ItemsSource="{Binding MyItemsSource}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate >
                <WrapPanel IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ListBox>
Run Code Online (Sandbox Code Playgroud)