1

I have been trying to create a Data Binding so that a WrapPanel automatically resizes horizontally to match it's container (a StackPanel) when the window is resized, to no success.

I started to search how to do it, and at I arrived to this

    Binding SomeBinding = new Binding ();
    SomeBinding.Source = SomeEntry;
    SomeBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    SomeBinding.Path = new PropertyPath ("Width");
    SomeBinding.Mode = BindingMode.OneWay;
    SomeStackPanel.SetBinding (StackPanel.WidthProperty, SomeBinding);

But it doesn't do anything when the window is resized. I looked at examples, but I don't really see the issue. Can someone explain what is wrong with the above snippet?

8
  • Why are you trying to set the binding on the StackPanel's Width property? You should set the binding on the WrapPanel's Width in my opinion, because that will be the binding's target. Also, set SomeBinding.Source to the StackPanel instance. Commented Jul 23, 2015 at 11:41
  • What is SomeEntry? Why use ReportBinding when you just created SomeBinding? All that shouldn't be necessary at all, since a WrapPanel inside a vertical StackPanel already stretches horizontally. Commented Jul 23, 2015 at 11:43
  • @Clemens Forgot to edit that Commented Jul 23, 2015 at 11:44
  • Please, show us your XAML. You shouldn't need a Binding like that, and it's hardly gonna work how you want. Your problem is a layouting one. Commented Jul 23, 2015 at 14:46
  • @almulo There isn't much to show: Just a Grid with a TextBlock and StackPanel inside. The idea is to populate the StackPanel at runtime and set this binding so things resize properly. Commented Jul 23, 2015 at 14:55

2 Answers 2

3

StackPanel do not expand to the size of their containers - they are the size of their contents. So if the WrapPanel is linked to StackPanel.Width, nothing will happen as the window grows.

You are probably looking for something else instead of a StackPanel - try to use a Grid.

Sign up to request clarification or add additional context in comments.

6 Comments

I wouldn't use a StackPanel if I could, but the issue is that I need to have every element stacked, and, as far I know, you can't do that easily with a Grid...
Oh yes you can do that easily.
@Andrei Rinea How? I've been looking into it for the past hour, but I didn't find anything...
How come you want to have your elements stacked, but then you want your WrapPanel to be the size of your StackPanel? That doesn't make much sense... Either the WrapPanel has its own Height, letting the other elements stack, or you use another type of Panel that is indeed able to resize its contents.
I have already given up on the WrapPanel, I am trying other Panels
|
1

You've said you want your WrapPanel to resize horizontally inside the StackPanel. That's possible depending on what your StackPanel's Orientation is.

If your StackPanel's Orientation is Vertical, then its just a matter of setting HorizontalAlignment="Stretch" on your WrapPanel.

<StackPanel Orientation="Vertical">
    <WrapPanel HorizontalAlignment="Stretch" />
</StackPanel>

Or since you're doing it programmatically, for what it seems...

var wrapPanel = new WrapPanel();
myStackPanel.Children.Add(wrapPanel);
wrapPanel.HorizontalAlignment = HorizontalAlignment.Stretch;

If your StackPanel's Orientation is Horizontal, then there's no way you're gonna be able to make your WrapPanel resize correctly in that direction.

2 Comments

I had already tried HorizontalAlignment.Stretch, it doesn't work for what I was trying to do.. I've only now discovered ItemCollection and ItemTemplate, and they sound like the correct way to go about what I was doing, rather than trying to do it all in the code behind, by hand, as I was doing now. Thanks for the help though.
Yeah, an ItemsControl sounds useful for what you're trying. But in the end, ItemsControl uses a StackPanel inside by default, anyway. So you may find the same problem. Make sure the ItemsControl (or StackPanel) stretch correctly before trying to make its contents stretch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.