WrapPanel作为ItemsControl的ItemPanel [英] WrapPanel as ItemPanel for ItemsControl

查看:143
本文介绍了WrapPanel作为ItemsControl的ItemPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍在与WPF混在一起,边走边学.现在尝试建立动态的控件分组(主要是按钮,但可能包括CheckBoxes和其他控件).

Still fooling around with WPF and learning as I go. Trying now to build a dynamic grouping of controls (mostly Buttons but might include CheckBoxes and others).

我不知道什么是最好的方法,所以我尝试创建ItemsControl样式,然后将项目添加到WrapPanel内的ItemsPresenter中.不久,我意识到这些项目不会被包装,因为除非我将其放置为ItemsHost,否则它们实际上不在WrapPanel内.像这样:

I had no idea what was the best way to do this so I tried creating a ItemsControl style and then add the items into a ItemsPresenter inside a WrapPanel. Soon realized the items wouldn't wrap because they effectively weren't inside the WrapPanel unless I put it as ItemsHost. Like this:

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

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

Note that this is a work in progress and there are many styling effects I still need to implement. Here I use it:

<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并不是要走的路,而且边距可能会很痛苦(是否有重复的边距?),因此仍在进行中.

Also note here that it is still a work in progress as UniformGrid wouldn't be the way to go here and also margins can be a pain (are there any margins that overlap?) so input there would be appreciated.

现在是真正的问题.这不起作用,我得到一个错误:

Now to the real problem. This doesn't work I get a error:

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

'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中,并把它们排列起来很不错).最好将控件放入某种类型的集合中并进行迭代.

So what's the best way to do something like this ( would love to be able to just throw buttons and other controls into the ItemControl and the line up real nice ). Would it be better to put the Controls into a collection of some kind and iterate.

很乐意很好地完成它,但是我的WPF技能仍然缺乏.是否有WPF书籍能讲授基础知识以外的其他内容,并显示出专业人士将如何真正做到这一点?

Would love to get it nicely done but my WPF skills are still lacking. Are there any WPF books that teach beyond the basics and show how pro's would really do it?

推荐答案

您可能想看看

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

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

示例:

<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

您可以按如下方式设置其样式:

And you can set it in a Style as follows:

<Style TargetType="ItemsControl">
    <Setter Property="ItemsPanel">
      <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

这篇关于WrapPanel作为ItemsControl的ItemPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆