I implemented the canvas from the code project example Drag controls in canvas
Here user has the ability to move anywhere in the canvas, when control reaches top or bottom, i want to show the vertical scrollbar. How can I make this work?
<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
<Grid Name="grid">
<Canvas Name="canvastoExport" >
<Polygon Points="200,100,300,100,300,200,200,200" Fill="Green"></Polygon>
</Canvas>
</Grid>
</ScrollViewer>
Related
I've programmatically generated buttons in a UniformGrid i.e. 4 rows and 4 columns, so 16 buttons.
I want to be able to click and drag to create a rectangle box to select a box of buttons.
Is this possible?
Trying to use this example currently but not seeing the drag box appear
Click and drag selection box in WPF
Currently, since it's all programmatic, the UniformGrid code in XAML is this:
<Canvas DockPanel.Dock="Top" Name="buttonCanvas" Width="800" Height="400">
<Rectangle x:Name="selectionBox" Visibility="Collapsed" Stroke="White" StrokeThickness="4" />
<UniformGrid DockPanel.Dock="Top" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="uniformGrid" Grid.Row="1" Width="800" Height="400"
Rows="{Binding RowNums}"
Columns="{Binding ColumnNums}" MouseDown="uniformGrid_MouseDown" MouseUp="uniformGrid_MouseUp" MouseMove="uniformGrid_MouseMove" Background="Transparent">
</UniformGrid>
<!-- This canvas contains elements that are to be selected -->
</Canvas>
Start by drawing a Canvas over the UniformGrid:
<Grid>
<UniformGrid>
<Button/>
...etc....
</UniformGrid>
<Canvas/>
<Grid>
Then you would by handle the mouse down, mouse move and mouse up events for the canvas. In mouse down you would add a rectangle to the canvas as the current mouse pos, in the mouse move you would resize the rectangle based on the current mouse pos and work out which buttons were enclosed by the rectangle and set them to be "selected" by binding to a view model somewhere. In the mouse up you would remove the rectangle. You would have some kind of trigger in a style to set the style to be different when selected
I have a more StackPanels in my XAML. Every StackPanel has a border inside.
When I modify the Main Window the width follows the resizing. But the height follows only in one the bigger direction. If I make the Window smaller the height of the borders doesn't follows. So the effect is the Botton border line isn't visible. How can I do this ?
<Window x:Class="MyStackPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyCombobox" Height="356" Width="475">
<Grid>
<StackPanel x:Name="STP"
Margin="10">
<Border x:Name="STPB"
BorderBrush="#FFE80707"
BorderThickness="5"
CornerRadius="10"
Height="{Binding ElementName=STP,Path=ActualHeight}"/>
</StackPanel>
</Grid>
</Window>
!border normaly Looks like
Use a Grid instead of a StackPanel then the border will stretch to the
Grids height and width on resizing
<Grid x:Name="STP"
Margin="10">
<Border x:Name="STPB"
BorderBrush="#FFE80707"
BorderThickness="5"
CornerRadius="10" />
</Grid>
Window after your changes, Looks like the same after reize (make smaller) without your changes.
http://i.stack.imgur.com/SplMn.jpg
I'm using StackPanels in Silverlight 5 (VS2012), how can I center them in screen? is there any property (horizontalalignemt & verticalalignment do nothing), or should I center them using code?
You have to put stack panel in a container like Grid control then horizontal and vertical alignment properties should work.
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel Width="50" HorizontalAlignment="Right"> </StackPanel>
</Grid>
I have the following code:
<Grid>
<Canvas Grid.Row="0" x:Name="drawingSurface" Background="White" ClipToBounds="True"
MouseLeftButtonDown="drawingSurface_MouseLeftButtonDown"
MouseLeftButtonUp="drawingSurface_MouseLeftButtonUp"
MouseMove="drawingSurface_MouseMove">
</Canvas>
<Grid Name="pnlProperties" Visibility="Hidden"/>
</Grid>
After starting this window, the user selects his interested area (I catch MouseMove, MouseLeftButtonDown, MouseLeftButtonUp).
Then I want to show the panel pnlProperties under the selected area in the left corner (in my interested coordinates).
How can I do it?
Put the Grid into the Canvas, and then set the coordinates for the Grid, using:
YourCanvas.SetLeft(pnlProperties, MOUSE.X)
YourCanvas.SetTop(pnlProperties, MOUSE.Y);
Since the canvas requires a Top/Left for placement, if you want to center something, is adding a grid at the proper Canvas.Top with HorizontalAlignment="Center" the best way to do it, or is there a better way?
This snip is a 150X300 canvas, with some content centered in a grid ....
<Canvas Width="150" Height="300">
<Grid Canvas.Top="75" Width="106" HorizontalAlignment="Center">
{whatever you want centered}
</Grid>
</Canvas>
Guy's solution works, but you may have to tweak z-order and visibility if you're juggling hit testing.
Another alternative is having the Grid inside the Canvas (as you've specified in your XAML) with the Height/Width set to (or bound to) the Height/Width of the Canvas. Then setting HorizontalAlignment/VerticalAlignment to Center for the contents of your Grid.
I'm not sure if this will meet your exact requirement, but if you put both the canvas and the content inside a grid as peers, it will get you a centered result:
<Grid>
<Canvas Width="150" Height="300"/>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Width="106" Content="Click"/>
</Grid>