简体   繁体   English

ItemsControl Windows Phone中的项目间距

[英]Item spacing within ItemsControl Windows Phone

I am generating buttons on the fly. 我正在动态生成按钮。 The List<Button> is bound to the an ItemsControl: List<Button>绑定到an ItemsControl:

<ItemsControl ItemsSource="{Binding MyButtons}" Padding="0" 
    Style="{StaticResource HorizontalStackPanel}" />

Here is the style definition: 这是样式定义:

<Style x:Key="HorizontalStackPanel" TargetType="ItemsControl">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            Margin="0"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

A button is generated using the following method: 使用以下方法生成按钮:

private Button MakeAButton(string letter)
{
    Button b = new Button
    {
        Margin = new System.Windows.Thickness(-2),
        CommandParameter = letter
    };

    b.Content = new TextBlock { Text = letter, FontSize = 24, FontFamily = new System.Windows.Media.FontFamily("Segoe UI Mono") };
    b.Click += b_Click;
    return b;
}

The number of buttons varies every time. 每次按钮的数量都不同。 I need them to be all visible on the phone page and be spread apart such that the user has an easy time pushing the buttons. 我需要它们在电话页面上都可见并且分散开,以便用户轻松按下按钮。

Problem: Buttons are spread out inside ItemsControl in such a way so that if there are more than 8 buttons in the collection, they start disappearing from the screen. 问题:按钮以这种方式散布在ItemsControl中,这样,如果集合中有8个以上的按钮,它们就会开始从屏幕上消失。 Controlling the space by setting the Margin on buttons works, but then I have to adjust it for every number of items in the collection. 通过在按钮上设置“边距”来控制空间是可行的,但是随后我必须针对集合中每个项目的数量对其进行调整。 Setting MaxWidth on the ItemsControl does not work. 在ItemsControl上设置MaxWidth无效。 I measured my buttons individually and the sum of all .DesiredSize does not exceed 250px. 我单独测量了按钮,所有.DesiredSize的总和不超过250px。

Questions: Is there a way to programmatically control the space between items within ItemsControl? 问题:是否可以通过程序控制ItemsControl中项目之间的空间? More importantly, what mechanism does ItemsControl use to space items within it? 更重要的是,ItemsControl使用什么机制来分隔其中的项目?

You should use a WrapPanel instead of a StackPanel . 您应该使用WrapPanel而不是StackPanel A WrapPanel allows the content to wrap around the screen and not go off the screen. WrapPanel允许内容环绕屏幕而不是离开屏幕。 You can use the WrapPanel within the Windows Phone Toolkit or Telerik's controls or any other third party WrapPanel. 您可以使用Windows Phone Toolkit或Telerik的控件中的WrapPanel或任何其他第三方WrapPanel。

If using the WP Toolkit, modify you style to be as such 如果使用WP Toolkit,请按照如下方式修改您的样式

<Style x:Key="HorizontalStackPanel" TargetType="ItemsControl">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <toolkit:WrapPanel />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM