0

I'm using a canvas to produce a hovering effect of my expander over elements below it and I bound its width to the expander's. However, I just noticed (when I changed the language) that one of the elements is a long string and it provides its width to the parent (the expander) when it's visible but not otherwise.

<Canvas Width="{Binding ActualWidth, ElementName=TheExpander}">
  <Expander Name="TheExpander" ...>
    <ListBox ... >
      <ListBox.ItemTemplate>
        <DataTemplate>
          <CheckBox ... />
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
  </Expander>
</Canvas>

Is there any relatively easy way to do that? If the correct solution requires me to rebuild the markup, I believe that I'll prefer to simply set the width of the canvas the ugly, nasty and slap-me-in-the-back-of-the-head way, i.e. hard-coded.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

1 Answers1

0

You should almost never hard code widths and heights. Let the WPF layout engine figure it out. That's what its there for. Something like a StackPanel, etc. will grow as needed.

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86
  • I agree that I should **almost** never do that. I prefer to have the width provided by the widest child but here it doesn't happen until the child actually is visible when the expander opens. I need to use the canvas because I need the hovering effect that I get by putting it above in z-index. Suggestions? – Konrad Viltersten Aug 17 '15 at 19:43
  • Are you trying to make a tooltip type thing? Post a screen shot of what you are trying to accomplish and then I might have a better suggestion, but based on the info I have now :), I would suggest a tooltip or popup control and fake it coming from the expander if you need them to click on a button to activate it. – SledgeHammer Aug 17 '15 at 20:13
  • I believe that the actual choice of the component to be used has been quite well discussed before, when I was making the choice. The screenshots and more info is [right here](http://stackoverflow.com/questions/28362036/how-to-bind-to-a-controls-actually-actual-width-including-its-margins). Because of a number of factors I'll be stuck with canvas but I'm trying to avoid the hard-coded width. When I changed the language, the widest child got wider than the expander's text. Is there any way to make the actual width to be the actual width **to come**? – Konrad Viltersten Aug 17 '15 at 20:24
  • Seems to me like you haven't defined how you want the layout to work. Do you want the combo to line up with those buttons and the buttons to size? Or do you want the combobox to show the full width of the text and then the date pickers to be spaced a bit over past that? If the later, then I'd use a StackPanel. If you make the expander a combobox, the drop down will show on top of the other stuff. The canvas simply doesn't work how you want it to. If you are determined to make it work, measure the width of all the strings and find the widest one and then add room for borders, checkbox, etc. – SledgeHammer Aug 17 '15 at 21:12
  • I recognize the trail of thoughts in your suggestion from ours the last winter. We've been trying out combo boxes, lists and all that. There was always some minor detail not being fixable in each of them, until we got the expander/canvas constellation. So now we're stuck with it. I understand that you say it's not possible to bind the width of the canvas to the actual width of the widest child (that might be not visible at the moment). If so, we'll have to go with the quick and dirty solution. Care to update your reply with that? So I can accept it as an answer, please. – Konrad Viltersten Aug 18 '15 at 04:57
  • I didn't say that. I said if you are stuck with your current implementation, then when strings are loaded / updated, simply loop through them and measure the width using the FormattedText class and keep track of the max width. That will allow you to adjust the width of the control to do what you want. Its not going to do it for you automatically the way you have it set up. – SledgeHammer Aug 18 '15 at 05:12