In a WPF application I'm developing, I intend to have a number of buttons on the left in a separate grid, however, when I resize the application (making it smaller) the button shrinks and eventually disappears.
I know there's obviously something I'm overlooking, but I can't find out what it is.
Here's the XAML for that grid:
<Grid HorizontalAlignment="Left"
Margin="0,23,0,0"
Name="pnlNav"
VerticalAlignment="Stretch"
Width="200">
<Button Content="Team Open"
Height="31"
Name="btnTeamOpen"
Width="144"
Click="btnTeamOpen_Click"
Margin="26,44,30,533" />
</Grid>
Your button has a ridiculous margin: Margin="26,44,30,533"
This means the button has to be over 500px from the bottom, clippling occurs if the available space is smaller than the top margin + height + bottom margin.
(MSDN article on the layout system & Alignment, Margins & Padding)
Related
I am trying to make responsive WPF app which shows image. One of the program's functionalities is selecting a piece of an image by clicking and draging the mouse. I use Point p = e.GetPosition((IInputElement)sender); to find cursor position, and I found out I cannot use Stretch="Fill" because it causes the MouseUp cursor to select a little lower than it should and MouseMove is also inaccurate (I have to drag the mouse a lot further than I should). On the internet, I found the reason for this behavior that you cannot use Fill and have to use None instead. However, the image is much smaller without Fill.
This is my XAML:
<Grid Grid.Row = "1"
Grid.Column="1"
HorizontalAlignment="Left"
VerticalAlignment= "Top"
Margin="0,30,0,0">
<Image x:Name= "image1"
Grid.Row = "1"
Grid.Column="1"
Cursor="Cross"
MinWidth="300"
MinHeight="300"
MaxWidth="512"
MaxHeight= "512"
Stretch = "None"
RenderOptions.BitmapScalingMode="NearestNeighbor"
RenderOptions.EdgeMode="Aliased"
VerticalAlignment="Top"
MouseDown="picOriginal_MouseDown"
MouseMove="picOriginal_MouseMove"
MouseUp="picOriginal_MouseUp" />
</Grid>
I don't know how to embed my image so that in the window view it fills the grid without this Fill property and at the same time is responsive for fullscreen. Should I wrap Image with something else from the WPF toolbox?
I find it easier using the background of the picture box to be the image then use the stretch in that. Alternatively, you could use the image with any of the other options, try using the the properties tab, here are the other stretch options tho: None, Fill, Uniform, UniformToFill
Tell me if this helps, tryna get rep, thanks!
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 am seeing inconsistent placement of my Popup control when my application is run on different computers. I have the Placement set to AbsolutePoint and experience the popup being aligned to popup's top right on 2 computers, but to the popup's top left on 1 other computer (when running the same application on each computer).
I am positioning the popup using the HorizontalOffset and VerticalOffset dependency properties, relative to the screen's top left coordinate.
The documentation (https://msdn.microsoft.com/en-us/library/bb613596%28v=vs.110%29.aspx) shows that the popup alignment point should be to the top left of the popup, with the target area being the whole screen (and so target origin in the top left of the screen).
Thinking that it might be a change in the .Net framework or a difference in display scaling factors (unlikely, but still), I gathered the following info, but I cannot see an obvious reason for this behaviour;
The computers where the popup alignment point is to the popup's top right are:
1).Net Framework 4.5.1 and scaling factor of 125%
2).Net Framework 4.5.2 and scaling factor of 100%
The computer where the popup alignment point is to the popup's top left is:
1).Net Framework 4.5.2 with an unknown scaling factor (I need access to it to check again).
Any ideas why the placement is inconsistent? It is not to do with the screen's boundaries - the popup is not near any edge.
I fixed this issue by adding a border in the same grid col/row as the desired placement target. Then set this as the placement target instead. By binding the width of this border to the popup content it will adjust it's width automatically therefore the alignment (left or right) is irrelevant. If you want to still control alignment, you can do that by aligning the placement target border. Hope that makes sense, if not, here is a quick example.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Popup x:Name="StartMenuPopup" Placement="Top" PlacementTarget="{Binding ElementName=PopupTarget}" >
<Border x:Name="PopupBorder">
</Border>
</Popup>
<Border x:Name="PopupTarget" Grid.Row="1" Width="{Binding ActualWidth, Mode=OneWay, ElementName=PopupBorder}"
BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<startmenu:TaskBar Grid.Row="1">
<startmenu:TaskBar.StartButton>
<startmenu:ToggleMenu Width="36" x:Name="StartButton"
ImageData="{Binding StartButtonImageData}"
AssociatedPopup="{Binding ElementName=StartMenuPopup}"
IsOpen="{Binding StartMenuOpen, Mode=TwoWay}"/>
</startmenu:TaskBar.StartButton>
</startmenu:TaskBar>
</Grid>
The popup PlacementTarget binds to the PopupTarget border, and the PopupTarget border width binds back to the PopupBorder element. This makes the PopupTarget border the same width as the popup therefore negating the alignment issue.
My xaml application is not filling up my screen when I debug it.
As you can see my buttons are displayed at the bottom.
After debugging the app the buttons are now half way up.
This is the positions that xaml has set as default, both are at auto auto.
Instead of using Auto, use Bottom for both.
If your buttons are in a stack panel, you can lock (anchor) the stack panel to the bottom of your LayoutRoot and set the height of the stack panel to a height close to the height of your buttons. Then, I would look at setting the LayoutRoot width and height to auto.
You can also configure your grid with rows and cols along with their dimensions.
try this xaml:
<Grid>
<StackPanel HorizontalAlignment="Center" Margin="0,278,0,0" Width="517" Orientation="Horizontal" Height="50" VerticalAlignment="Bottom">
<Button Content="Button"/>
<Button Content="Button"/>
</StackPanel>
</Grid>
On your grid you'll see little locks positioned at the center of ever side of the grid.
If you add an object and move your mouse along the locks your cursor changes to a hand pointing a finger icon. If you click on the lock at that point, you'll see the lock either locks or unlocks. (i.e. you'll see a larger gap between the lock lines). It helps maintain the anchor position of the object when resizing the screen.
This is a screenshot with the stack panel selected. Notice that only the bottom anchor is locked.
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.