views:

152

answers:

5

Inserting a Slider in a Grid would expand it to fill the available space, but I would prefer not use a grid for the following reason:

I've a TextBlock and a Slider in a UserControl, the slider is spring loaded and does jog / shuttle; the current value has to be displayed because the user can't rely on the neutral cursor's position, so the textblock. Implementing the **Orientation** property of this custom slider requires both components to be rotated and also their relative position to be adjusted (left/right or top/bottom), which wouldn't be easy with a grid (unless I miss something obvious) while it is with a StackPanel.

Response to Aviad's comment

Aviad, thanks, I apologize for the pain ;-) The question was in the title: How to expand a Slider to fill the available space when the slider is within a StackPanel ?

This user control:

<UserControl x:Class="XXX.Preview.SelectionView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="auto" Width="auto">
    <GroupBox Header="Selected">
        <StackPanel Orientation="Horizontal">
            <TextBlock/>
            <Slider/>
        </StackPanel>
    </GroupBox>
</UserControl>

won't expand when included in a grid even in a row with a "*" width. The slider will have no length at all.

A solution is to replace the stack panel by a grid in the code below, but I don't want to use a grid, because I need to use the Orientation property of the stack panel to show both controls stacked vertically when the enclosing user control is set in the Orientation "Vertical".

A: 

This thing can be better done by using DockPanel instead of StackPanel. You can try that.

Check this: http://msdn.microsoft.com/en-us/library/bb628664.aspx

and http://www.wpftutorial.net/DockPanel.html

viky
+1  A: 

The short answer is "you can't." StackPanel is limited this way by its ArrangeOverride and MeasureOverride code. It simply does not have the ability to expand to fill available space in the stacking direction.

You could subclass StackPanel, of course, and implement it differently, but StackPanel is so simple that if you're going to do that you may as well just subclass Panel, or you could subclass DockPanel (which has the expansion ability), and set DockPanel.Dock on all its children based on its StackPanel.Orientation property:

public class StackPanelExpanding : DockPanel
{
  public Orientation Orientation
  {
    get { return (Orientation)GetValue(OrientationProperty); }
    set { SetValue(OrientationProperty, value); }
  }
  public static DependencyProperty OrientationProperty =
    StackPanel.OrientationProperty.AddOwner(typeof(StackPanelExpanding));

  protected override MeasureOverride(...)
  {
    var dock = Orientation==Orientation.Vertical ? Dock.Top : Dock.Left;
    foreach(var element in Children)
      SetDock(element, dock);
    base.MeasureOverride(...);
  }
}

You still didn't make it clear why you can't just use a DockPanel and use trigger in your template to synchronize the Orientation and DockPanel.Dock. Frankly in most cases I would be more inclined to go that way than to create a custom panel.

Ray Burns
A: 

Thanks for having pointed out DockPanel which is what I need. I overlooked this possibility... lacking skills. Ray I appreciate the alternative. Stackoverflow.com is a great site.

slide
You can leave comments (like this one) on your own questions and answers, and on answers to your own questions. See the FAQ for more information. http://stackoverflow.com/faq
Bill the Lizard
A: 

To Marc Gravell: "stop creating accounts". hu, I've no account. The only way I may be identified is by my email, which may vary from time to time ;-) as I don't want to be tracked. In the FAQ: "Do I have to log in or create an account? Nope. You can answer and ask questions to your heart's content as an anonymous user". Marc is there anything wrong with that?

To Bill the Lizard: "You can leave comments (like this one) on your own questions and answers, and on answers to your own questions". Bill, I'd like to do that, included this last message from me. I'm aware that user the answer method is not appropriate. The FAQ says: "If other users ask you for more information in the comments, edit your question using the edit link just below your original question.". The problem is that I don't see any edit link under my question, nor can't find this text in the page content using FF search. Maybe this is limited to registered users?

I apologize for any inconvenience created.

slide
OK, but even as an anonymous user you can stay logged in - up to you, but because you keep logging out (or dumping your cookies) and re-creating, the system doesn't know that you are you, hence no "edit" etc. Plus you won't be able to accept an answer, which is the polite thing to do. Do what you like, of course.
Marc Gravell
A: 

Marc, thanks for the explanation. My computer isn't accepting cookies. Though I'm thinking about allowing them specifically for stackoverflow.com, I'll likely stay with my current network config. This is sad because I like this site and the value of the contributions. Since without being identified, I can only post answers as a new user, in big font characters, I'll stop sending them to not clutter the page with comments like this one, irrelevant to the initial technical question.

slide