Size of Grid is NaN, wpf [duplicate] - wpf

This question already has an answer here:
Why is my Grid's width NaN?
(1 answer)
Closed last year.
I want to get size of Grid, but I get NaN..
Here is the xaml file:
<Window x:Class="TestWPFHCI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestWPFHCI"
mc:Ignorable="d"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
Title="MainWindow" Height="700" Width="1024" MinHeight="700" MinWidth="1024">
<Grid Background="#FFEEEEEE">
<ScrollViewer x:Name="scrollOnGrid" Margin="280 55 10 10" HorizontalAlignment="Stretch" VerticalAlignment="Top" HorizontalScrollBarVisibility="Auto">
<!--<Grid Name="MCGrid" Margin="100 100 50 50">
</Grid>-->
<Grid Width="Auto" Margin="10 10 10 10" HorizontalAlignment="Stretch" VerticalAlignment="Top" x:Name="MCGrid">
<!-- <Grid x:Name="MCGrid"/> HorizontalAlignment="Center" VerticalAlignment="Center"/> -->
</Grid>
</ScrollViewer>
</Grid>
</Window>
And here is the code for get grid size, but I always get NaN.. Any idea for solution?
int width = MCGrid.Width;

Use MCGrid.ActualWidth to get the width of Grid.
double width = MCGrid.ActualWidth;

Related

Wpf textbox cannot enter numbers and letters

The keyboard cannot enter English and numbers
window style
I have previously set a prohibition on the use of input methods " xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"
input:InputMethod.IsInputMethodEnabled="False"
But now it’s deleted and you can’t enter numbers and English
<Window
x:Class="WS_StationManagerPlugin.Forms.FrmMPTable"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cr_utilitywpf="clr-namespace:CR_UtilityWPF;assembly=CR_UtilityWPF"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WS_StationManagerPlugin.Forms"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="FrmTbale"
Width="800"
Height="600"
Loaded="Window_Loaded"
mc:Ignorable="d">
<TextBox
x:Name="tbxCurrentPage"
Width="30"
Margin="5,0,5,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="#002C41"
Foreground="#fff"
KeyDown="tbxCurrentPage_KeyDown"
PreviewTextInput="tbxCurrentPage_PreviewTextInput"
TextAlignment="Center">
<TextBox.ToolTip>
<ToolTip Content="当前页" />
</TextBox.ToolTip>
</TextBox>
</Window>

Why is the content of this label not centered? [duplicate]

This question already has an answer here:
Getting text to sit flush against the top of a TextBlock (Not VerticalAlignment, I promise!)
(1 answer)
Closed 2 years ago.
Consider the following xaml:
<Window x:Class="DemoApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="DemoApp.MainWindow" Height="450" Width="800">
<StackPanel Orientation="Horizontal">
<Label BorderThickness="1" BorderBrush="Black" Width="100" Height="100" FontSize="120" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Padding="0">A</Label>
</StackPanel>
</Window>
As shown in the image below, there is plenty of space within the label to accomodate the letter, yet it appears that the letter does not center as per the VerticalContentAlignment. What is the cause of this?
I think VerticalContentAlignment has no problem.
Problem is that your Label is smaller than Font size.
<Label BorderThickness="1" BorderBrush="Black" Width="100" FontSize="120" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Padding="0">A</Label>
Try this code.
I removed code "Height=100" and height is auto.

initial size of element wpf

I have window with this markup
<Window x:Class="TestCloseWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Button Command="ApplicationCommands.Close">Foo</Button>
</Window>
I'd like to button have MinWidth and MinHeight 100
and MaxHeight and MinHeight equals to 500
And like size window to content so I make this
<Window x:Class="TestCloseWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" SizeToContent="WidthAndHeight" >
<Button MaxHeight="500" MaxWidth="500" MinHeight="100" MinWidth="100" Command="ApplicationCommands.Close">Foo</Button>
</Window>
And I'd like to set initial size of button 200 x 200
without writing Width and Height in window
If I write Width="200" Height="200" in button, button becomes unresizable.
How can I set initial size of control?
I am not sure exactly what you are trying to accomplish, but see if this does what you are wanting. I am using a ViewBox to stretch the Contents when the Window is resized, and setting the contraints in the Window. Be aware that Window Size is different than Window Client Area size.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" MaxHeight="500" MaxWidth="500"
MinWidth="100" MinHeight="100"
Width="200" Height="200">
<Viewbox StretchDirection="Both" Stretch="Fill">
<Button >Hello</Button>
</Viewbox>

Window set to size around content leaves gap on each side

Can someone please tell me why, even though I've set my Window to SizeToContent and my content is Width=40 does the app load bigger across the Width?
The following example sizes to height, but the width is about 100 pixels (estimation).
Thanks
<Window x:Class="WpfApplication2.RetailButs"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="RetailButs" SizeToContent="WidthAndHeight"
WindowStyle="None" MinWidth="0" >
<Grid x:Name="LayoutRooty" Width="40" Height="40">
<Border x:Name="LayoutRootx" Background="White" BorderBrush="Black" CornerRadius="8" BorderThickness="4" >
</Border>
</Grid>
</Window>
The extra size you have got comes from 3 top right window buttons.
So the best solution is just take buttons out (1) and put some minimum size for the window (2).
Try this and it will be exactly what you need.
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Width="40" Height="40" SizeToContent="WidthAndHeight" WindowStyle="None" ResizeMode="NoResize">
<Grid x:Name="LayoutRooty" Width="40" Height="40">
<Border x:Name="LayoutRootx" Background="White" BorderBrush="Black" CornerRadius="8" BorderThickness="4" >
</Border>
</Grid>
</Window>
There was a similar question recently which i was unable to find. The size is that which would be needed if the WindowStyle were the default 3D border with all the buttons which take up a lot of space. I think there is some way to update the layout, maybe you can figure something out.
Edit: Set the window's Width="0" (or any other value for that matter) and it should update correctly. The size to content will override the value.
(If you set the WindowStyle to ToolWindow it always fits correctly)

how stretch user control in tab control item

Hi I try solve how stretch user control width / height which is active in tab control to tab control width / height.
I use caliburn micro.
I create some user control. Here is it:
<UserControl x:Class="Spirit.Views.TabChatView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Micro="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
xmlns:Controls="clr-namespace:Spirit.Controls" mc:Ignorable="d"
Name="ChatWindow"
Background="{StaticResource LightGrayBackground}"
Height="545" Width="680">[...]</UserControl>
This user control is active in shell, shell declaration is here.
<Window x:Class="Spirit.Views.ChatShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
Title="ChatShellView" Height="300" Width="300">
<DockPanel>
<Button x:Name="OpenTab"
Content="Open Tab"
DockPanel.Dock="Top" />
<TabControl x:Name="Items">
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding DisplayName}" />
<Button Content="X"
cal:Message.Attach="CloseItem($dataContext)" />
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
</DockPanel>
</Window>
Result if some user constrol is active in this shell you can see on this image:
I would like stretch user control on maximum tab control witdh / height.
Remove Height="545" Width="680" in UserControl definition

Resources