Buttons in the corners? - wpf

I'd like to have 4 buttons one in each corner of a WPF/Silverlight window. But I want the stuff in the grid/window to be "behind" the buttons, as if they float on top.
<Grid x:Name="ButtonRoot">
<Button VerticalAlignment="Top" HorizontalAlignment="Left"
Name="bTopLeft" />
<Button VerticalAlignment="Top" HorizontalAlignment="Right"
Name="bTopRight" />
<Button VerticalAlignment="Bottom" HorizontalAlignment="Left"
Name="bBottomLeft" />
<Button VerticalAlignment="Bottom" HorizontalAlignment="Right"
Name="bBottomRight" />
<!-- Other junk here -->
</Grid>
The problem is, the buttons will not be "over" things, as the things will "wrap" around the buttons. How do I achieve this effect?
Diagram of how I want it http://dl.compdj.com/images/stackButton.jpg

Use two grids, remember whatever is farther down the file will be on top:
<Grid>
<Grid Background="Green"><!-- put stuff here --></Grid>
<Grid><!-- this goes on top -->
<Button Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" Content="Button" /><!-- top left button -->
<!-- etc -->
</Grid>
</Grid>

this should solve your problem
#bTopLeft {
position: absolute;
top: 0;
left: 0;
z-index: 1200;
}

Related

Canvas position based on another element

Basically I want a Canvas to act like a StackPanel. So how do set the Canvas.Top="" based on Canvas.Bottom of another element?
Background: Trying to make an Expander that when expanded it will go over other elements. Figured using a Canvas ZIndex would be the best way to do this. So I created this:
<StackPanel Orientation="Vertical" >
<Canvas Height="{Binding ActualHeight, ElementName=MyExpander}">
<Expander Panel.ZIndex="1" Name="MyExpander" Header="Header" >
<StackPanel Background="LightGray">
<TextBlock Text="Some text" />
<TextBlock Text="Some text" />
<TextBlock Text="Some text" />
<TextBlock Text="Some text" />
<TextBlock Text="Some text" />
<TextBlock Text="Some text" />
</StackPanel>
</Expander>
<StackPanel Panel.ZIndex="0" Canvas.Top="20" Margin="0,5,0,0">
<Button Content="Button1" />
<Button Content="Button2" />
<Button Content="Button3" />
</StackPanel>
</Canvas>
</StackPanel>
Now this works perfectly the problem is that Canvas.Top="20" is hardcoded into the XAML. So that means if the font gets increased (the user increased font sizing in Windows) then part of the StackPanel will be under the Expander. I tried this:
Canvas.Bottom="{Binding (Canvas.Bottom),ElementName=MyExpander}"
The issue being is that value for for Canvas.Bottom for MyExpander is NaN so that isn't going to work.
FYI if there is a better way to do the Expander expands over top of elements I am open to that as well.
thanks
Well this doesn't seem like the greatest answer but it worked for me.
foreach (var exp in MyCanvas.Children)
{
Expander ep = (Expander)exp;
double h = ep.ActualHeight;
Canvas.SetTop(ep, y);
y = y + h + 20;
}
Should note it goes without saying that foreach loop is a bit more complicated due to correct parsing of the objects in the MyCanvas

control not positioning right

I wantto postion control like this pic
I need Next and previous buttons to be as pic 1, but i get the following, and not able to solve this.
Please guide, XAML code:
<StackPanel Grid.Row="4">
<Grid x:Name="GridNavigation">
<Button x:Name="Gridprevbutton" Content="Previous" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="121,0,0,0"></Button>
<Button x:Name="Gridnextbutton" Content="Next" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="231 , 0, 0, 0"></Button>
<Button x:Name="Gridendexambuton" Content="End Exam" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,5,0"></Button>
</Grid>
<TextBlock HorizontalAlignment="Right" Margin="0,5,10,0" TextDecorations="Underline">Contact Support</TextBlock>
</StackPanel>
(Edit) I end up doing this following #davisoa advise. and got it working.
Code behind
private void SetNavigation()
{
var img = Common.AddImageToContainer("images/lowerleftblock.png", GridNavigation, Common.ToDictionaryTLST("top", "left", "none"));
img.Margin = new Thickness(0,16,0,0);
Panel.SetZIndex(img, -1);
}
Give your image a top margin to bring it down by however much is needed.
E.g.
<Image x:Name="Lowerleftblock" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,20,0,0"></Image>
It looks to me like the Grid you are not showing above (the first StackPanel is in Grid.Row="4") is limiting the height of your Next and Previous buttons.
This example XAML shows the Question block lower than the buttons.
Changed Image to a StackPanel with a Label, since I don't have your image
Changed VerticalAlignment to Bottom, to ensure it floats on the bottom of the Grid
Gave the Grid a fixed Height to ensure the vertical layout is correct. You probably don't want to do this
<StackPanel Grid.Row="4">
<Grid x:Name="GridNavigation" Margin="0,0,0,0" Height="75">
<StackPanel x:Name="Lowerleftblock" VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="0,0,0,0" Background="Green">
<Label>Question: 1 of 27</Label>
</StackPanel>
<Button x:Name="Gridprevbutton" Content="Previous" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="121,0,0,0"></Button>
<Button x:Name="Gridnextbutton" Content="Next" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="231 , 0, 0, 0"></Button>
<Button x:Name="Gridendexambuton" Content="End Exam" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,5,0"></Button>
</Grid>
<TextBlock HorizontalAlignment="Right" Margin="0,5,10,0" TextDecorations="Underline">Contact Support</TextBlock>
</StackPanel>

Trigger Silverlight button mouseover event for two buttons simultaneously

I have two sets of buttons in two wrappanles that are related to each other. When I move the mouse over a button in one wrappanel, I want its related button to light up at the same time i.e. firing both buttons' mouseover events. So in my code exampes, I when moving over button with label "1.Block", it and its matching button in the other row, "1.Sqaure", must both display their mouseover styles/animations.
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="400" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition Height="60" />
</Grid.RowDefinitions>
<toolkit:WrapPanel Grid.Column="0" Grid.Row="0">
<Button Content="1. Block" Width="100" />
<Button Content="2. Block" Width="100" />
<Button Content="3. Block" Width="100" />
<Button Content="4. Block" Width="100" />
<Button Content="5. Block" Width="100" />
<Button Content="6. Block" Width="100" />
<Button Content="7. Block" Width="100" />
<Button Content="8. Block" Width="100" />
</toolkit:WrapPanel>
<toolkit:WrapPanel Grid.Column="0" Grid.Row="1">
<Button Content="1. Square" Width="100" />
<Button Content="2. Square" Width="100" />
<Button Content="3. Square" Width="100" />
<Button Content="4. Square" Width="100" />
<Button Content="5. Square" Width="100" />
<Button Content="6. Square" Width="100" />
<Button Content="7. Square" Width="100" />
<Button Content="8. Square" Width="100" />
</toolkit:WrapPanel>
</Grid>
I have no idea where to start on this? Trigger perhaps? Any suggestions will be appreciated.
I think the easiest way to do this is to use VisualStateManager. Following example may be a little rough but should be sufficient overall.
So what we need is to link MouseEnter and MouseLeave events for each pair of buttons in two lists. I don`t have WrapPanel so I used StackPanels instead, but this should not matter if we can get all child buttons from them. Say we have this:
<StackPanel x:Name="LayoutRoot" Orientation="Horizontal">
<StackPanel Name="panel1">
<Button Content="1. Block" Width="100" />
<Button Content="2. Block" Width="100" />
</StackPanel>
<StackPanel Name="panel2">
<Button Content="1. Square" Width="100" />
<Button Content="2. Square" Width="100" />
</StackPanel>
</StackPanel>
Using panels by name we take all their children (this can be done in constuctor for example) and iterate through them assigning same event handlers that move both buttons to required state:
var panel1Buttons = panel1.Children.OfType<Button>().ToList();
var panel2Buttons = panel2.Children.OfType<Button>().ToList();
for (int i = 0; i < panel1Buttons.Count(); i++)
{
var button1 = panel1Buttons[i];
var button2 = panel2Buttons[i];
//assign same mouse enter event handler
MouseEventHandler enterHandler = (s, e) =>
{
VisualStateManager.GoToState(button1, "MouseOver", true);
VisualStateManager.GoToState(button2, "MouseOver", true);
};
button1.MouseEnter += enterHandler;
button2.MouseEnter += enterHandler;
//assign same mouse leave handler
MouseEventHandler leaveHandler = (s, e) =>
{
VisualStateManager.GoToState(button1, "Normal", true);
VisualStateManager.GoToState(button2, "Normal", true);
};
button1.MouseLeave += leaveHandler;
button2.MouseLeave += leaveHandler;
}
So now when mouse enters any of paired buttons they both go to MouseOver state and return to Normal on when mouse leaves.
This us purely UI related functionality so even if you follow MVVM it`s ok to place this in code behind.

How to align the bottom edge of multiple TextBlocks with different FontSize

Here is the XAML sample:
<StackPanel Orientation="Horizontal" >
<TextBlock VerticalAlignment="Bottom" Text="Text1" FontSize="20" />
<Image VerticalAlignment="Bottom" ... />
<TextBlock VerticalAlignment="Bottom" Text="Text2" />
</StackPanel>
The result is that the textblocks have different bottom-margins depending on their FontSizes, but I need them all to be on one bottom line with no margins. How to get it? In my case I cant use TextBlock + Runs.
Forget several blobks : the bottom position dépends of the font size.
But, in a single paragraph, write the flow of your objects
You should use something like this :
<RichTextBlock>
<Paragraph>
<Span FontSize="148">Hello</Span>
<InlineUIContainer>
<Image Source="Assets/Logo.png"></Image>
</InlineUIContainer>
<Span FontSize="36">again</Span>
</Paragraph>
</RichTextBlock>

Align button content

I want the content of a wpf button to be left aligned
I tried the following but the text is still centered inside the button.
<Button >
<StackPanel HorizontalAlignment="Stretch">
<TextBlock HorizontalAlignment="Left" Text="Save"/>
</StackPanel>
</Button>
What do i do?
Found it, it's HorizontalContentAlignment.
:)
You don't need the StackPanel or the TextBlock. All you need is
<Button HorizontalContentAlignment="Left" Content="Save" />
You can align in two way, Horizontal and Vertical
<Button Content="Export" VerticalContentAlignment="Bottom" name="MyBtn1" />
or
<Button Content="Export" HorizontalContentAlignment="Center" name="MyBtn2" />
See the follow image to view the possibilities settings:

Resources