I have an issue with a WPF C# project, where I have a StackPanel on my Form, within that I have a DockPanel which has items added to it programmatically, the problem is that the items are going out of the form's bounds and I would like to scroll through these, I already have a method for "Scrollifying" the items, but for the life of me I can't see why the scrollbars aren't appearing on the screen, am I missing something?
Code is below, any suggestions are more than welcome.
private void ScrollifyStackpanel(StackPanel Panel)
{
ScrollViewer SV = new ScrollViewer();
SV.Height = Panel.ActualHeight;
SV.Width = Panel.ActualWidth;
SV.CanContentScroll = true;
SV.Content = Panel;
}
Xaml
this is the stackpanel containing the (should be) scrollable content
<StackPanel Name="divParentPanel" VerticalAlignment="Top" DockPanel.Dock="Left" Margin="10" Width="Auto" HorizontalAlignment="Stretch" CanVerticallyScroll="True">
Specify ScrollBar Visibility:
ScrollViewer SV = new ScrollViewer();
SV.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
SV.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
Sometimes it can happen that ScrollViewer can't detect the necessity of ScrollBars at exact moment of size change. It's totally
context specific but set the visibility to Visible to be safe always.
#Reece Try by setting the height for stack panel. since StackPanel cannot have dynamic height. Or else you can use Grid instead of StackPanel
Related
When I create a new ScrollViewer, I need to modify the size of the ScrollBars in the ScrollViewer (change the VerticalScroll width and the HorizontalScroll height), programatically.
I tried the following based in a solution found in SO, without success:
public static ScrollViewer CreateScrollViewer()
{
ScrollViewer result = new ScrollViewer();
// I need to set the scroll width and height here
// I tried the following with no success
result.Resources.Add(SystemParameters.VerticalScrollBarWidth, 100);
return result;
}
I saw some solutions to change them, but all are XAML based. I need to do it in runtime, in pure C#. How could I do this?
You can access the ScrollBars from the ScrollViewer's ControlTemplate. You can find out how to do that from the How to: Find ControlTemplate-Generated Elements page on MSDN and you can find details of the default ControlTemplate in the ScrollViewer Styles and Templates page on MSDN, but in short, try this:
ScrollViewer scrollViewer = new ScrollViewer();
scrollViewer.ApplyTemplate();
ScrollBar scrollBar =
(ScrollBar)scrollViewer.Template.FindName("PART_VerticalScrollBar", scrollViewer);
You can do the same for the horizontal ScrollBar which is named PART_HorizontalScrollBar.
I have a TextBox that I'm creating, via code, to insert into a Control in a WPF interface. The Control can be dynamically resized via click-and-drag code, and I want the TextBox's height and width to match the Control's. The end result is that I have a TextBox that I can dynamically resize.
The box is going to be placed in this Border:
<Border Name="borInner" Margin="0,0,2,0" HorizontalAlignment="Center" VerticalAlignment="Center" />
I'm trying to make the textbox fill the area of the border. To do so, I'm assuming 2 things have to happen.
I have to set Horizontal and Vertical Alignments on borInner to Stretch
The TextBox must also have those properties.
Unfortunately, when both of these things are set, the TextBox matches the Width of the overall control, but not the height. It just sits centered vertically still. Does the Height value of a TextBox control just not follow Stretch? If I set a height manually, it will work, but I'd rather not have to manually update the size of the textbox during the mousemove if I can help it, as it's pretty slow to calculate what the new dimensions should be constantly.
For more detail on the Control the TextBox is being inserted into: It has a property I've made called bool FitContentToSize, which makes this code happen on the Control's creation
borInner.VerticalAlignment = VerticalAlignment.Stretch;
borInner.HorizontalAlignment = HorizontalAlignment.Stretch;
I then create the textbox
element = new TextBox() {
AcceptsReturn = true,
TextWrapping = TextWrapping.Wrap,
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
};
and add that TextBox to borInner.
What am I missing, or does anyone have a decent idea for a workaround?
Try setting the height of the textbox to Double.NaN and then setting the margin to 0 all around.
Make sure your inner border (borInner) is not inside something like a StackPanel, or anything else that limits the height of its children to the bare minimum. If this is the case, consider using something like a Grid, or DockPanel.
I have an application that works with different tabs. At left side I have my menu and at the middle I have DockPanel. According to menu selection I am adding a windowbase window as child to my dock panel and with mouse ball scroll but it does not scroll. Normally when you are over scrollviewer area it automatically triggers scrolling but for dock panel its just moving when I am over scroll bar not on whole dock panel.
Thanks guys I was thinking that when I add scrollviewer into the DockPanel and clear children than the scrollvier also be removed. But it is not like that.At the beggining I was using it like following code
<ScrollViewer x:Name="innerScrool" VerticalScrollBarVisibility="Auto" VerticalAlignment="Top" MinHeight="350"><DockPanel Margin="0,10,0,0" VerticalAlignment="Top" x:Name="ControlPanelContent">
But I change the code take the scrollviewr inside and the problem solved.
I want to put lots of controls inside a StackPanel and user can scroll up and down. I have a simple StackPanel inside a grid.
<Grid>
<StackPanel>
</StackPanel>
</Grid>
If I'm not wrong, I've seen that StackPanel scrolls, but now it is not working. Does it support scrolling or it is not supported at all? ( I prefer not to put it inside a ScrollViewer). thanks
Nothing really scrolls unless it has a ScrollViewer inside. When you do it like you're doing it now, you'll see only the items that fit on the screen, and others will be 'out of screen borders'.
So I'm afraid you'll have to add a ScrollViewer. It's actually a good thing that StackPanel doesn't scroll by default.
I have an ItemsControl that looks something like this:
<ItemsControl ItemsSource="{Binding}" PreviewMouseWheel="ItemsControl_PreviewMouseWheel" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0,4"><!--Margin to keep the items from being smashed too closely together-->
...
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The ItemsControl will be one of several items in a scrollable area, but if I use the mouse wheel over the ItemsControl, nothing happens. So I want to forward the mouse wheel events further up the tree:
private void ItemsControl_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
RaiseEvent(new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
{
RoutedEvent = UIElement.MouseWheelEvent
});
}
This mostly works. But when the mouse scrolls over the Margin area (specified by Margin="0,4") between two items in the control, nothing happens. I tried putting the grid inside a decorator, such as a Border, but that doesn't seem to help. How can I capture these mouse wheel events, and forward them up the tree?
The Margin is the area of space left around a Control, and since it's just empty space it doesn't process any events.
An alternative is to either nest controls, such as putting your Grid with the Margin inside a DockPanel without a Margin, or to use a WPF control that has the Padding property such as Border