Gre*_*ski 22 .net wpf xaml itemscontrol
我正在尝试编写一个简单的WPF学习项目,该项目在可调整大小的主窗口中创建一组按钮.还有就是要成为一个Button每一个集合中的条目,该集合的内容可能会在运行时有所不同.我希望按钮能够填满整个窗口(例如1个按钮@ 100%宽度,2个按钮@ 50%宽度,3个按钮@ 33%宽度等等都在100%高度).我到目前为止所编写的简化版本是:
<ItemsControl x:Name="itemscontrolButtons" ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Tag="{Binding}">
<TextBlock Text="{Binding}" />
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
...
List<string> listButtonTexts = new List<string>() { "Button1", "Button2" };
...
itemscontrolButtons.DataContext = listButtonTexts;
Run Code Online (Sandbox Code Playgroud)
这导致:

我一直无法使按钮拉伸以适应宽度,我尝试使用Grid而不是StackPanel没有结果.
然后,作为一个可选的改进,我将很感激如何调整它的建议,如果有这么多的按钮,他们不能正确地适合在一条线上或比一个阈值窄,它将包裹到一个新的线(从而减半按钮高度,如果从1到2行).
我想强调一下,我想知道如何以WPF的方式做到这一点.我意识到我可以使用窗口调整大小的消息并显式调整控件的大小,这就是我用MFC或WinForms完成它的方式,但从我读过的内容并不是如何用WPF完成的.
Nir*_*Nir 55
将项目控件的面板替换为UniformGrid,这将调整所有子项的大小,以便所有内容完全适合:
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="1"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13437 次 |
| 最近记录: |