How do I position an element within a WPF Grid column? - wpf

Let's say I want to position an element at the coordinates x=20, y=5 within the 3rd column of a Grid control. How do I do this? Do I need to add a Canvas panel to the column and then add the controls to it?

You can do it that way, but have a look at this..
Attached Property
<Grid Background="Yellow">
<Rectangle Fill="Red" Margin="20,10,0,0" Width="50" Height="30"
HorizontalAlignment="Left" VerticalAlignment="Top" />
<Rectangle Stroke="Green" Margin="30,15,0,0" Width="20" Height="30"
HorizontalAlignment="Left" VerticalAlignment="Top" />
</Grid>
You can use the Margin property to simulate absolute positioning.
Not sure whether this would help you, but in any case an interesting read.

Related

Control blocking input to lower control

Hopefully this is a WPF issue and not a esri issue. I have an ESRI map control, and I am placing a control on top of the map. When I do this, the map no longer receives input(I can't move around or click anything). If I modify the z-index of the top control to place it behind the map, the map works fine again.
<Grid Name="MapGrid" DockPanel.Dock="Top" >
<Grid Name="MapGrid" DockPanel.Dock="Top" >
<esri:MapView x:Name="MainMapView" Panel.ZIndex="0" KeyDown="MyMapView_KeyDown" MapViewTapped="MyMapView_MapViewTapped" Map="{Binding Source={StaticResource MapResource}}" WrapAround="True" Grid.Row="0" Grid.Column="0" Initialized="MyMapView_Initialized" >
</esri:MapView>
<Expander Header="Properties" ExpandDirection="Right" Panel.ZIndex="-1">
<ItemsControl Background="Transparent" Height="700" Width="500" HorizontalAlignment="Left" VerticalAlignment="Top" Name="FeaturePropertyRegion" cal:RegionManager.RegionName="FeaturePropertyRegion" />
</Expander>
</Grid>
This code works, but if I raise the ZIndex of the Expander pannel, the map no longer receives input. I am assuming the issue has to do with the visual tree of WPF, and how input cascades down. Any ideas on what the issue could be? Thanks.
EDIT
The issue seems to be with the expander, as the map works if I remove the expander and just have the ItemsControl.
Try :
<Expander IsHitTestVisible="False">
<ItemsControl />
</Expander>
It doesn't seem to be a problem with extender, which would be very wired if it did
I tried this :
<Grid>
<Button />
<Expander HorizontalAlignment="Left" ExpandDirection="Right" VerticalAlignment="Top">
<Rectangle Fill="Red" Stretch="Fill" Width="525" Height="350"/>
</Expander>
</Grid>

Silverlight Drag and Drop across Canvas or StackPanel

My question is about to drag and drop functionality in Silverlight 5
<Grid Canvas.Left="75" Canvas.Top="130" Background="Transparent" Width="595" MaxWidth="595" Canvas.ZIndex="4" >
<StackPanel x:Name="MainSP" Background="Transparent" Orientation="Horizontal" HorizontalAlignment="Center">
<StackPanel x:Name="StackPanelGroup1" Tag="Group1" MinHeight="129" Orientation="Vertical" VerticalAlignment="Bottom" HorizontalAlignment="Center">
<Canvas x:Name="CanvasGroup1" Tag="Group1" HorizontalAlignment="Stretch" >
<Button x:Name="ToGroup1" Tag="Group1" Cursor="Hand" Click="ToGroup1_Click" Height="20" Canvas.Left="0" Canvas.Top="20" Content="Add Here" Visibility="Collapsed" Canvas.ZIndex="4" />
<!--<this:CustomCard x:Name="myCustomCard" Canvas.Left="0" Canvas.Top="0" HorizontalAlignment="Center" DiscardCard="myCustomCard_DiscardCard" FinishCard="myCustomCard_FinishCard" MouseLeftDown="myCustomCard_MouseLeftDown" >
</this:CustomCard>-->
</Canvas>
</StackPanel>
</StackPanel>
I am having XAML structure like thi, there will will up to 7 group for StackPanel and Canvas also.
Now wehen I will get 13 card from server in different group I will dynamically add them to respective canvas group.
Issue is MainSP is 7 group of StackPanel and Canvas now their Canvas's default ZIndex property will be assigned autometically like CanavsGroup1 has 1, and CanvasGroup2 has 2 ans so on...
When I want to drag card from CanvasGroup3 it is draggable all over screen and CanvasGroup 1 and 2 but when I tries to drag it over CanvasGroup4 or 5 it's not working.
I think it's ZIndex property issue.
I tried somany things to give Zindex property of Card and Canvas to max zindex property but then also it's not working.
Is there any solution that I can move card without using Canvas? or bring that Card to front(top) while dragging?

WPF Dockpanel won't align right

I have this code on which I use a DockPanel on a Button.Content. However it doesn't let me right align the last image (small arrow).
<Button Height="70"
HorizontalContentAlignment="Left">
<Button.Content>
<DockPanel LastChildFill="False">
<Image DockPanel.Dock="Left"
Height="50"
Width="50"
Source="{StaticResource Placeholder}"
Stretch="UniformToFill"
Margin="5" />
<TextBlock DockPanel.Dock="Left"
Text="Dummy text"
VerticalAlignment="Center"
Margin="5" />
<Image DockPanel.Dock="Right"
Height="24"
Width="24"
Source="{StaticResource Right_Arrow_24}"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Stretch="UniformToFill"
Margin="5" />
</DockPanel>
</Button.Content>
</Button>
It now gives me this:
So the right small arrow should be placed on the right of the button and not just after the TextBlock.
I've found some similar issues and it looks like I did it correct, but somehow it isn't..
What am I doing wrong here?
Try to set the HorizontalContentAlignment of your button to "Stretch". Otherwise your DockPanel will use the size need by its content, then align left. You can confirm this behaviour by using different text lengths for your TextBlocks
You simply have to append an extra child to the DockPanel (say an empty Canvas) without the DockPanel.Dock attribute, so that all the remaining space is assigned to it.
In general, DockPanel only works fine only when its last child has no Dock constraint
Try setting
HorizontalContentAlignment="Stretch"
On your button.

WPF Item binding

i am trying to bind to rectangle with each other. so when ever a rectangle move the second one move, i am still new to WPF!!! and i know this might sounds really stupid all help are really welcome. please find below my code and help me pls
<Canvas Height="500" Width="500" Name="Window1canvas"
Background="BLUE"
DnD:DragDropManager.DragSource="{StaticResource sourceForDragOp}"
DnD:DragDropManager.DropTarget="{StaticResource targetForDragOp}">
<Rectangle Name="Rec1" Width="50"
Height="50"
Fill="Yellow"
Canvas.Left="251"
Canvas.Top="288"/>
<Rectangle Width="50"
Height="50"
Fill="Green"
Canvas.Left="{Binding ElementName=Rec1, Path=(Canvas.Left), Mode=TwoWay}"
Canvas.Top="100"/>
</Canvas>
Try to define rectangles position in view model and bind it to rectangles using TwoWay binding.

how to extend WPF GridRow Contents into next row without expanding the current row

I'm new to WPF and I'm trying to build a dropdown menu using the expander. Page layout is being handled with a Grid.
The extender sits inside the first row of the grid and I would I would like the contents of the expander to expand over top of the contents of everything below when it's clicked. Unfortunately, right now, the entire row is expanded to accommodate the height of the expanded control.
I tried playing around with the ZIndex of the Expander but it doesn't seem to have any effect. No matter what, the row always expands forcing everything else on the page to move.
<Expander FontSize="18" Name="moduleSelect" Width="100" Header=" Goto -> "
Background="#000033" MouseEnter="moduleSelect_MouseEnter"
MouseLeave="moduleSelect_MouseLeave" Foreground="White"
Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1" HorizontalAlignment="Left">
<StackPanel>
<Button Name="btnTasks" Width="100" Foreground="White"
Background="#000033">Tasks</Button>
<Button Name="btnNotes" Width="100" Foreground="White"
Background="#000033">Notes</Button>
</StackPanel>
</Expander>
How can I make this expand 'above' the subsequent rows without distorting the grid?
What would happen if you set the Grid.RowSpan of the Expander to 2 (or how ever many rows you'd like it to span when expanded)?
So, for a two-row grid, you'd have something like this:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" /> <!--set this to the height of the expander's header area-->
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<WhateverGoesInRow2 Grid.Row="1" />
<Expander FontSize="18" Name="moduleSelect" Width="100" Header=" Goto -> "
Background="#000033" MouseEnter="moduleSelect_MouseEnter"
MouseLeave="moduleSelect_MouseLeave" Foreground="White"
Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Left"
Grid.Row=0 Grid.RowSpan="2">
<StackPanel>
<Button Name="btnTasks" Width="100" Foreground="White" Background="#000033">Tasks</Button>
<Button Name="btnNotes" Width="100" Foreground="White" Background="#000033">Notes</Button>
</StackPanel>
</Expander>
</Grid>
You may need to adjust your RowDefinition section for your particular situation, but if I'm understanding your problem correctly, I think this will work.
You want something that pops up over the grid, not expands within the grid. A ComboBox, say, or - this being a menu, after all - a ContextMenu.
You could also build some combination of a ToggleButton and a Popup, but that's essentially the same thing as a ComboBox with IsEditable turned off.
The built-in drop-down control makes use of a Popup control in its default control template to do a similar thing.

Resources