拉伸控制,以填补ItemsControl的 [英] Stretching controls to fill ItemsControl

查看:171
本文介绍了拉伸控制,以填补ItemsControl的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个简单的WPF的学习项目,创建一组按钮内的可调整大小的主窗口。还有就是有一个按钮每一个集合条目,该集合的内容可能会在运行时有所不同。我想要的按钮来填满整个窗口(如1按钮@ 100%的宽度,2个按钮@ 50%的宽度,3个按钮@ 33%的宽度等方面都以100%的高度)。什么我写至今的简化版本如下:

I am attempting to write a simple WPF learning project which creates a set of buttons inside a resizeable main window. There is to be one Button per entry in a collection, and the contents of that collection may vary during run-time. I want the buttons to fill the entire window (e.g. 1 button @ 100% width, 2 buttons @ 50% width, 3 buttons @ 33% width, etc. all at 100% height). A simplified version of what I've written so far is:

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

这会导致这样的:

我已经无法使按钮延伸到适合的宽度和我尝试使用电网而不是的StackPanel 是徒劳的。

I have been unable to make the buttons stretch to fit the width and my attempts to use a Grid instead of StackPanel were fruitless.

然后,作为一个可选的改进,我会如何调整它使得如果有这么多的按钮,他们不能适当地容纳在一条线或小于一阈值窄,它将换到AP preciate建议一个新行(从而如果减半按钮高度从1到2排去)。

Then, as an optional improvement, I would appreciate suggestions on how to adjust it such that if there are so many buttons that they cannot fit properly on a line or are narrower than a threshold, it will wrap onto a new line (thereby halving the button heights if going from 1 to 2 rows).

我想强调的是,我想知道如何做到这一点的的 WPF的方式的。我知道我可以消耗窗口调整消息,并明确地调整控制,这就是如何我会用MFC或WinForms的做了,但是从我读过,也不怎么这样的事情应该用WPF做的。

I'd like to emphasize that I'd like to know how to do this the WPF way. I realize I can consume window resize messages and resize the controls explicitly, and that's how I'd have done it with MFC or WinForms, but from what I've read that is not how such things should be done with WPF.

推荐答案

更​​换项目控制的面板采用了UniformGrid,这将大小所有的孩子,所以一切完全符合:

Replace the item control's panel with a UniformGrid, this will size all children so everything fits exactly:

<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="1"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

这篇关于拉伸控制,以填补ItemsControl的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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