views:

806

answers:

4
A: 

Your last paragraph suggested WrapPanel to me: have you tried that?

serialhobbyist
Indeed, I tried replacing the StackPanel with a WrapPanel, unfortunately, then it wouldn't stretch either horizontally or vertically. I'm hoping I can apply the solution to my main issue to a WrapPanel to solve my last paragraph, but it's not that critical so I wanted to focus on solving it without wrapping first. Thanks.
Greg
Yeah, sorry. I tested it after I posted instead. Next time I'll test first. Sorry. I guess you don't want to define columns up front: <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Button Grid.Column="0">Button 1</Button> <Button Grid.Column="1">Button 2</Button> <Button Grid.Column="2">Button 2</Button> </Grid>
serialhobbyist
Unless there's some way to "template" that, I don't believe that will work here since the number of buttons can vary dependent on the bound collection.
Greg
How about: <DockPanel LastChildFill="False"> <Button>Hello</Button> <Button>Hello2</Button> <Button>Hello3</Button> </DockPanel>
serialhobbyist
Perhaps I'm misunderstanding you, but I'm not sure how that applies here since the buttons cannot be pre-defined. In my simplified example above I just bound to a List<string> but a more realistic solution would be binding to ObservableCollection<string> or something that similarly implements INotifyCollectionChanged. Then whenever I modify the bound collection at run-time, the window will adjust the buttons layout automatically.
Greg
A: 
<!-- width must be explicitly set for this example -->
<StackPanel 
    Name="MyStack" 
    Orientation="Horizontal"
    Width="250"
    Load="Window_Loaded"> 
    <Button/>
    <Button/>
    <Button/>
</StackPanel>


public void Window_Loaded(object sender, RoutedEventArgs e)
{
    UIElementCollection elements = MyStack.Children;
    int count = elements.Count;

    foreach (UIElement element in elements)
    {
        if (element is Button)
            ((Button)element).Width = MyStack.Width / count;
    }
}
golden_eagle
I edited my post only 20 minutes before you responded to explain that I wanted to avoid explicitly setting the width. I'm sorry I should have thought to include that requirement initially. Explicitly managing the dimensions like this (and by handling resize messages as well) may be necessary for my secondary requirement, if WrapPanel were to be used. Thanks.
Greg
+5  A: 

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>
Nir
That was just what I needed for my primary goal, thank you.
Greg
Thanks to Nir's answer, I was able to fulfill the optional criterion. Details are in the answer I've posted to this question. It was recommended on Meta that I handle this situation this way. http://meta.stackoverflow.com/questions/14306/my-improved-answer-based-on-anothers-accepted-answer-for-my-own-question
Greg
A: 
Greg