I have a simple StackPanel on a grid and I want buttons within that stack panel to take up all space available (space between them should be the same).
In case it's not clear, there's a pic:
<StackPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Margin="10" Orientation="Horizontal">
<Button MinWidth="115">OK</Button>
<Button MinWidth="115">Cancel</Button>
<Button MinWidth="115">Cancel</Button>
</StackPanel>
Is it possible or do I have to just enter margins manually?
Thanks!
Make your StackPanel into a Grid instead (you'll need to refactor what you currently have slightly), and evenly space out all of the columns (using a width of *), and then set each button's alignment to left/center/right respectively, which will achieve the layout you're looking for.
Related
XAML:
<Grid Height="22" VerticalAlignment="Top" Background="#FF1368BD" >
<Button x:Name="ButtonPowerOff" HorizontalAlignment="Right" Height="22" Width="22" Background="{x:Null}" BorderBrush="{x:Null}" Click="ButtonPowerOff_Click">
<materialDesign:PackIcon Width="15" Height="15" Kind="WindowClose" HorizontalAlignment="Center" Margin="0 0 0 0" ></materialDesign:PackIcon>
</Button>
</Grid>
You can try setting the height and width of the button to "auto" and also set the padding to "0" in the button. Also delete the HorizontalAlignment of the icon.
I don't know what library is PackIcon, and that's is important because I don't know it behaviour. But I can tell...
Why do you set the PackIcon size if you have set the Button size?
The problem here is button maybe has default padding making the "effective content" size of the button smaller than image, and the image is doing what you want but "the space needed is greater than i have".
Let the image stretch in the button (without setting the size of it) or let the button fit the image 15x15 size (without setting the size of the button).
Everything in WPF is ready for sizeless/locationless layout. Except Canvas children, of course; and some bizarre behaviour of Image control.
If you can avoid use Width and Height in your controls (sometimes you can't) you should do it. Instead use margins, alignments and paddings. This way you let the window and controls be totally responsive.
I have a grid of many checkboxes, they are 70 height and 47 width. For a single checkbox, I want the checkbox's check mark box to stay visible and fully opaque on top of whatever is within the checkbox itself. In this case the only content of the checkbox will be an image (it is the same size 70height 47width) that is moved so that it lines up with the whole box. However the image's opacity covers the check mark box.
As of right now I have this (it isn't what I want so much as shows the problem). The opacity of the image is there so you can see the image and checkbox. The margin puts the image over the whole checkbox
<CheckBox Grid.Column="0" Grid.Row="1" Opacity="1" BorderThickness="0" Unchecked="CheckBoxChange" Checked="CheckBoxChange" TabIndex="7" IsTabStop="True">
<Image Source="Resources/1.png" Width="47" Height="70" HorizontalAlignment="Right" Stretch="None" VerticalAlignment="Center" Margin="-18,1,0,0" Opacity=".4"/>
</CheckBox>
If you do a similar format of this for a button it works fine but I need try this with checkbox because it far better fits the use of the function needed.
Is there a way to do what I've explained or do I need to make something custom?
What I want is to have a page with a Textblock in the middle. Also I want the page to be clickable (as in you should be able to click anywhere on the entire page and the function will be called).
What I've tried is to have the TextBlock in a Viewbox and to set stretch to full, but that made the textblock text out of proportion. And if I set it to Uniform it only takes up however much space it needs. So either way it doesn't work.
Also the textblock needs to automatically resize itself when the page gets bigger / smaller (i.e. set Height and Width to "Auto"), I've tried a lot and nothing works, so any help is appreciated. Below is what I currently have.
<Viewbox x:Name="MainViewbox" Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="1, 1, 1, 1">
<TextBlock x:Name="MainTextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" Text="6" Height="Auto" Width="Auto" Foreground="LightSkyBlue"/>
</Viewbox>
This Almost works but I can only click on the six, not on the whole screen.
Anyway thanks in advance!
Figured it out!
All I had to do was add a pointerReleased event handler to the page, rather than the Viewbox
I have this:
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<StackPanel Orientation="Horizontal">
<StackPanel Width="100" Margin="20">
<CheckBox x:Name="cbFoo1" Content="Foo"/>
<TextBlock Text="Foo"/>
<TextBox />
</StackPanel>
<StackPanel Width="100" Margin="20">
<CheckBox x:Name="cbFoo2" Content="Foo"/>
<TextBlock Text="Foo"/>
<TextBox />
</StackPanel>
</StackPanel>
<Rectangle Fill="#BF000000" x:Name="rOverlay"/>
</Grid>
</Window>
I want to make a function foo with argument argControls() that makes rOverlay cover every control in the window except argControls(). Does anyone have any idea how to do this?
What about setting the visibility of the control to Collapsed when you want to hide them.
Option 1 - Draw Order
While a bit of a hack, perhaps the simplest way to accomplish this is to place controls that you wish covered before the rectangle and controls that you wish visible after the rectangle.
One step better (and not a hack) would to use the Panel.ZIndex property instead. This allows you to control the order that the UI elements are drawn regardless of the order that they are defined. This would be accomplished by setting the Panel.ZIndex as follows:
rOverlay to some arbitrary value (like 1)
controls to be covered to something lower than rOverlay (like 0)
controls to be visible to something higher than rOverlay (like 2)
It should however be noted that any transparent regions of your controls will still display the black rectangle behind them. This can either be specifying a background colour or by using another option presented here.
Option 2 - Clipping
Another more complex method involves setting the Clip property of the rectangle to a geometry that only includes the regions that you wish filled. This would likely be accomplished by using an instance of CombinedGeometry class with the CombineMode of Exclude. This would prevent the rectangle from drawing anything over regions outside the clipped region.
I got a pretty weird behavior of my WPF application: the XY position of my button on runtime seems to be divergent to that when I set it in my xaml-Editor of Visual Studio (is there a name for it btw?)
It has no alignments set or panels around it, i have only set it by margins. My button has the following code:
<Button Content="OK" Height="23" Margin="213,319,4,7" Name="button3" Width="75" IsCancel="True" Click="button3_Click" IsEnabled="False" />
Edit:
The margins are fixed because it is a non-resizable dialog. As you can see, the button's slightly moved to the left and up:
xaml-Editor:
Runtime:
Why is that and how can I fix it?
I guess the below link about the Alignment, Margins, and Padding Overview will help you to understand how it is works?
Else place a panel wrappers such as Stackpanel, Wrappand or Grid. It's suitable to work the layout of the controls
EDIT : The problem was with the ResizeMode="NoResize". If you remve this attribute in Window tag, then alignment would be good
Link to Refer
Man, that's the worst way to set the position of a UI element in WPF!
Refactor your XAML to something like this:
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListView Grid.Row="0" Name="TableList" Margin="5"/>
<Button Grid.Row="1" Name="button3" Content="OK"
Margin="5"
Width="75"
HorizontalAlignment="Right"/>
</Grid>
You see? There is a Grid that handles the position of all its children (in this case, a ListView and a Button).
The Button is put on the second Row, aligned to the right (HorizontalAlignment property).
Both the Grid and its children have Margin=5. This guarantees that the margin of every children is equal respect to the adiacent children and to other controls outside the Grid.
Also, the ListView and the Button are perfectly aligned.
The problem with your approach is that you set the Button Width and its Left Margin and its Right Margin. Maybe the total is not correct because the border of the Window eats some pixel, or simply WPF can't handle all the informations together and misses the calculation, who knows, but the consequence is that you must leave at least one parameter free. In my example, I left free the Margins from the Window. The Margin=5 sets only the relative Margin respect to the other controls, but how much the Button is distant from the left border of the Window is something I leave to the WPF graphical engine to calculate.