如何在ListBox或ItemsControl中创建按钮控件列表以在WPF中水平显示它们 [英] How to make a list of button controls in the ListBox or in the ItemsControl to Show them horizontally in WPF

查看:80
本文介绍了如何在ListBox或ItemsControl中创建按钮控件列表以在WPF中水平显示它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF的新手,并开始学习它。

我想从一个类的列表中做出按钮列表,说按钮并有两个字段(ButtonContent,ButtonID)在WPF的MainForm中。

这背后的想法是,当加载MainForm然后我想动态地制作按钮列表。

最好的例子已经在链接

动画中的交互式2D元素3D面板 [ ^ ]

但是我只想让水平堆叠的大小相等的按钮。

如何在WPF中执行此操作?提前谢谢。

I am new in the WPF and started learning on it.
I want to make the list of buttons from a list of a class say "Buttons" and having two fields (ButtonContent,ButtonID) in the MainForm in WPF.
The idea behind this is that when the MainForm will be loaded then i want to make buttons list dynamically.
The Best Example is already given in the Link
Animating Interactive 2D Elements in a 3D Panel[^]
But i only want to make the equal size buttons stacked horizontally.
How can i do this in WPF? Thank you in advance.

推荐答案

试试这个

Try this
<listbox.itemspanel>
         <itemspaneltemplate>
             <stackpanel orientation="Horizontal" verticalalignment="Top" />
         </itemspaneltemplate>
     </listbox.itemspanel>


对其他人来说也很方便。我在这里给出我的解决方案。这将有助于在ItemsControl或ListBox中动态制作按钮。

经过一番努力,我能够按照自己的意愿去做。
我的XAML中的
我确实喜欢这个。





It will be handy for others also. I am giving my solution here. This will help to make buttons dynamically in a ItemsControl or ListBox.
After some working hard i was able to do it as i wanted.
in my XAML i did like this.


<itemscontrol x:name="ic"><br />
    <itemscontrol.itemtemplate><br />
        <datatemplate><br />
            <Button<br />
                Command="Open"<br />
                CommandParameter="{Binding Path=ID}"<br />
                Cursor="Hand"<br />
                Focusable="False"<br />
                ><br />
                <Button.Template><br />
                    <controltemplate targettype="Button"><br />
                        <border><br />
                            removed="Black"<br />
                            BorderBrush="#88000000"<br />
                            BorderThickness="0.5"<br />
                            Padding="1"<br />
                            ><br />
                            <dockpanel><br />
                                <Button<br />
                                    Background="Transparent"             <br />
                                    Command="{Binding Open}"<br />
                                    CommandParameter="{Binding Path=ID}"<br />
                                    Content="{Binding Path=Content}"<br />
                                    DockPanel.Dock="Top"<br />
                                    Focusable="False"<br />
                                    Foreground="White"<br />
                                    HorizontalAlignment="Stretch"<br />
                                    Margin="0"<br />
                                    ToolTip="Testing Buttons..."<br />
                                        Click="Button_Click"<br />
                                    /><br />
                                <dockpanel.commandbindings><br />
                                    <commandbinding command="Open" executed="Open_Executed" /><br />
                                </dockpanel.commandbindings><br />
                            </dockpanel><br />
                        </border><br />
                    </controltemplate><br />
                </Button.Template><br />
            </Button><br />
        </datatemplate><br />
    </itemscontrol.itemtemplate><br />
</itemscontrol>





然后代码背后我做了如下:





then code behind i did like the following:

buttonContentList = MainButtonsList.FindAll(o => (o.MenuLevel == levelID) && (o.GroupID == groupID) && (o.Enable == isEnable));<br />
                this.ic.Items.Clear();<br />
                int buttonIndex = 0;<br />
                foreach (ButtonContent bc in buttonContentList)<br />
                {<br />
                    Button btn = new Button();<br />
                    btn.Click += new RoutedEventHandler(btn_Click);<br />
                    btn.Content = bc.Content;<br />
                    btn.Tag = buttonIndex++;<br />
                    btn.Height = 60;<br />
                    btn.Width = 80;<br />
                    btn.Margin = new Thickness(10, 5, 0, 5);<br />
<br />
                    this.ic.Items.Add(btn);<br />
                }


这篇关于如何在ListBox或ItemsControl中创建按钮控件列表以在WPF中水平显示它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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