Stretch WPF Dockpanel contents horizontally - wpf

I have a DockPanel and it contains a ScrollViewer [ center aligned ] and a button on left and right .
My xaml is like
<DockPanel LastChildFill="True">
<Button VerticalAlignment="Stretch" HorizontalAlignment="Stretch" DockPanel.Dock="Left">Left</Button>
<Button VerticalAlignment="Stretch" HorizontalAlignment="Stretch" DockPanel.Dock="Right">Right</Button>
<ScrollViewer Name="scrollAreaPageView" HorizontalAlignment="Center" VerticalAlignment="Center"
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
</ScrollViewer>
</DockPanel>
And it generates the output as expected , but Left and right butons are not stretched fully to left and right to the ScrollViewer( They are on corners only).
The screen shot of output is
How can i make it stretch fully to left and right of center scrollViewer

A DockPanel may not be ideal in this scenario you may perhaps use Grid here with the desired column definition
sample
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button>Left</Button>
<ScrollViewer Name="scrollAreaPageView"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Column="1">
</ScrollViewer>
<Button Grid.Column="2">Right</Button>
</Grid>
in above example the space available after subtracting the space required b
Alternate approach
I attempted to do it pure xaml, this approach will helo you achieve the desired without code behind. here is the example
<Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="{Binding ActualWidth,ElementName=scrollAreaPageView}" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button>Left</Button>
<Button Grid.Column="2">Right</Button>
</Grid>
<ScrollViewer HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="50,0"
Name="scrollAreaPageView"
HorizontalScrollBarVisibility="Auto">
</ScrollViewer>
</Grid>
Margin in the scrollAreaPageView element defines minimum width of the buttons. give it a try and see if that helps

Related

Define two column in wpf (left column should take whole area if right column is collapsed or take half area if right column is visible)

Define two column in wpf,each column has a GridControl (left GridControl should take whole area if right GridControl is collapsed or take half area if right GridControl is visible)
I try to set ColumnDefinition (Width=auto or Width=*) but do not achieve the expectation
Try this:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Border BorderBrush="HotPink" BorderThickness="1">
<TextBlock Text="Left Grid"/>
</Border>
</Grid>
<Grid Grid.Column="1" Visibility="Collapsed">
<Border BorderBrush="HotPink" BorderThickness="1">
<TextBlock Text="Right Grid"/>
</Border>
</Grid>
</Grid>

Silverlight: GridSplitter inside ScrollViewer behaving unexpectedly

I'm trying to build a two-column layout where the width of the columns can be changed by using a splitter. The right column's width shouldn't change when the browser window is resized (it shouldn't be proportional to the grid width). Both columns should have a minimum width. When the browser window is too narrow to display both columns a scrollbar should appear.
This is my XAML:
<Grid x:Name="LayoutRoot" Background="White">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
<Grid Height="200" Margin="0,0,0,0" MinWidth="400" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="300" Width="300*" />
<ColumnDefinition Width="10" />
<ColumnDefinition MinWidth="100" Width="100"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Background="Red" x:Name="LeftColumn"></Grid>
<sdk:GridSplitter Grid.Column="1" HorizontalAlignment="Center" Width="10" Background="Black" />
<Grid Grid.Column="2" Background="Green" x:Name="RightColumn"></Grid>
</Grid>
</ScrollViewer>
</Grid>
The problem I'm having is when the splitter is dragged to the left and the left column's minwidth is reached - the right column begins to grow very rapidly and the scrollbar appears. Removing the width setting from the right column eliminates the weird behavior but now the right column starts to grow proportionally when the window is resized...
I'd like the splitter to behave the same way as when it is dragged to the right - I'd like it to stop after the minwidth is reached.
You should disable the "HorizontalScrollBarVisibility".
This code works for me:
<Grid x:Name="LayoutRoot" Background="White">
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled">
<Grid Height="200" Margin="0,0,0,0" MinWidth="400" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="300" Width="300*" />
<ColumnDefinition Width="10" />
<ColumnDefinition MinWidth="100" Width="100"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Background="Red" x:Name="LeftColumn"></Grid>
<sdk:GridSplitter Grid.Column="1" HorizontalAlignment="Center" Width="10" Background="Black" />
<Grid Grid.Column="2" Background="Green" x:Name="RightColumn"></Grid>
</Grid>
</ScrollViewer>
</Grid>
The ScrollViewer gives the grid endless space to grow. Hence the minWidth never stops it.
Obviously there is no need of ScrollViewer that is disabled both vertically and horizontally. Better move the scroll bar inside the grid surrounding the content of every column.
I think I was finally able to come up with a workaround. I'm forcing the width in the code-behind when the layout is changing.
XAML:
<Grid x:Name="LayoutRoot" Background="White">
<ScrollViewer x:Name="Scroller" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
<Grid x:Name="Workspace" Height="200" Margin="0,0,0,0" MinWidth="400" VerticalAlignment="Top" LayoutUpdated="Workspace_LayoutUpdated">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="300" Width="300*" />
<ColumnDefinition Width="10" />
<ColumnDefinition MinWidth="100" Width="100"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Background="Red" x:Name="LeftColumn"></Grid>
<sdk:GridSplitter Grid.Column="1" HorizontalAlignment="Center" Width="10" Background="Black" />
<Grid Grid.Column="2" Background="Green" x:Name="RightColumn"></Grid>
</Grid>
</ScrollViewer>
</Grid>
Code behind:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void Workspace_LayoutUpdated(object sender, EventArgs e)
{
Workspace.Width = Scroller.ViewportWidth - 1;
}
}
The reason it behaves this way is that you have specified the first column Width="300*" with asterisks,
and the third column Width="100" without asterisks.
Just put asterisks to the first and third columns, or remove respectively, and it will work the way you wish.

How to create empty expanding grid col in expander header

I'm trying to create a custom expander header that will show one text box on the left, and several others on the right, and as the windows grows horizontally the left and right text blocks get further apart. It feels like this should work, but everything stays left justified.
<Expander.Header>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" Name="LeftJustifiedCol" ></ColumnDefinition>
<ColumnDefinition Width="*" Name="EmptySpaceCol" ></ColumnDefinition>
<ColumnDefinition Width="Auto" Name="RightJustifiedCol" ></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Margin="5,0,5,0" Text="{Binding GroupByValue}"></TextBlock>
<DockPanel Grid.Column="2" HorizontalAlignment="Right">
<TextBlock DockPanel.Dock="Right" Margin="5,0,5,0" Text="Net: $0.00"></TextBlock>
<Button DockPanel.Dock="Right" Margin="5,0,5,0" Width="Auto" Name="DeleteEntityBtn" Click="DeleteEntityBtn_Click" >Del</Button>
</DockPanel>
</Grid>
</Expander.Header>
any suggestions?
I am guessing WPF isn't rendering the middle column because it contains no content, so it takes up no space in the UI. Perhaps a DockPanel with LastChildFill="False" would work better than a Grid

WPF Scrollviewer won't scroll up

I have the following (simplified) xaml:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="143*" />
<ColumnDefinition Width="135*" />
</Grid.ColumnDefinitions>
<TabControl Margin="12,29" Name="tabControl1" Grid.ColumnSpan="2">
<TabItem Header="TabItem1" Name="tabItem1">
<ScrollViewer Height="440" Name="scrollViewer1" Width="872" HorizontalAlignment="Left" VerticalAlignment="Top">
<Grid Height="440" Name="grid1" Width="851" VerticalAlignment="Top" HorizontalAlignment="Left">
...
My problem is the scroll won't show up. What should I change ?
Thanks.
The content inside the ScrollViewer is smaller than the ScrollViewer itself. The ScrollViewer is used for being able to use scrollbars to show content that is larger than the area the ScrollViewer takes up.

ZOrder behavior in WPF grid?

The code below contains a simple grid with a button in grid's middle column. The button width is (by intention) larger than column with it is placed into. Notice that the left part of the button is visible the right one is not. What do I have to do to get both left and right button parts invisible? Right part of the button is z-below the right grid column, but the left part of the button is z-above the left grid column. I need the left button part also to be hidden by the left grid column.
This is a simplified version of XAML where I am trying to animate kind of 'film strip'. Film should be placed z-below left and right grid column and z-above the middle part. The animation works nicely, but user see for a while controls on the left part she is not supposed to see as those should be 'covered' by the left grid column.
<Grid x:Name="LayoutRoot">
<Border Background="Yellow" x:Name="ContentBorder">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
<ColumnDefinition />
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1" >
<Button Content="Button" Margin="-20, 0, 0, 0" Width="240" Height="33"/>
</Grid>
</Grid>
</Border>
</Grid>
Try adding a ClipToBounds
<Grid x:Name="LayoutRoot">
<Border Background="Yellow" x:Name="ContentBorder">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
<ColumnDefinition />
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1" ClipToBounds="True" >
<Button Content="Button" Margin="-20, 0, 0, 0" Width="240" Height="33"/>
</Grid>
</Grid>
</Border>
</Grid>

Resources