如何在水平Silverlight列表框中均匀分隔项目

时间:2021-03-04 21:38:30

In Silverlight, I have a Vertical ListBox that has a Horizontal ListBox for each item. I want the items in the HorizontalListbox to space evenly across the width of the parent (Vertical) ListBox. How can I do this?

在Silverlight中,我有一个Vertical ListBox,每个项目都有一个Horizo​​ntal ListBox。我希望Horizo​​ntalListbox中的项目在父(Vertical)ListBox的宽度上均匀分布。我怎样才能做到这一点?

   <ListBox x:Name="MachineListBox" Background="Green">
        <ListBox.ItemTemplate>
            <DataTemplate>
            <ListBox ItemsSource="{Binding CoilList}" Background="Red" HorizontalAlignment="Stretch">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                        <StackPanel 
                            HorizontalAlignment="Stretch"  />
                    </ItemsPanelTemplate                                >
                            </ListBox.ItemsPanel>

                    <ListBox.ItemTemplate >
                       <DataTemplate>
                                <TextBlock
                                    Text="{Binding Coil}"
                                     HorizontalAlignment="Stretch"/>
                        </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </DataTemplate>
            </ListBox.ItemTemplate>
    </ListBox>

2 个解决方案

#1


0  

I'll take the liberty of suggesting you use a custom control of my own invention. It's called a ProportionalPanel and it does just what you need - spaces items evenly. You could use it for the ItemsPanel in the inner ListBox instead of the StackPanel. I also provide the source code, so you can tweak the logic any way you like. The relevant post on my blog is here.

我冒昧地建议你使用我自己发明的自定义控件。它被称为ProportionalPanel,它可以满足您的需求 - 均匀地放置物品。您可以将它用于内部ListBox中的ItemsPanel而不是StackPanel。我还提供了源代码,因此您可以按照自己喜欢的方式调整逻辑。我博客上的相关帖子就在这里。

#2


0  

I think the proportional sizing operator will do what you're looking for. Haven't tried it but it sounds like an option. Width="*" and Margin="2*".

我认为比例尺寸调整操作符可以满足您的需求。没试过,但听起来像是一个选择。宽度=“*”和边距=“2 *”。

#1


0  

I'll take the liberty of suggesting you use a custom control of my own invention. It's called a ProportionalPanel and it does just what you need - spaces items evenly. You could use it for the ItemsPanel in the inner ListBox instead of the StackPanel. I also provide the source code, so you can tweak the logic any way you like. The relevant post on my blog is here.

我冒昧地建议你使用我自己发明的自定义控件。它被称为ProportionalPanel,它可以满足您的需求 - 均匀地放置物品。您可以将它用于内部ListBox中的ItemsPanel而不是StackPanel。我还提供了源代码,因此您可以按照自己喜欢的方式调整逻辑。我博客上的相关帖子就在这里。

#2


0  

I think the proportional sizing operator will do what you're looking for. Haven't tried it but it sounds like an option. Width="*" and Margin="2*".

我认为比例尺寸调整操作符可以满足您的需求。没试过,但听起来像是一个选择。宽度=“*”和边距=“2 *”。