<Grid.RowDefinitions>
<RowDefinition Height="4*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
I want to define 4/3 ratio somewhere else in XAML, and then use it. Something like this:
<System:Double x:Key="Top_Part">4</System:Double>
<System:Double x:Key="Bottom_Part">3</System:Double>
<Grid.RowDefinitions>
<RowDefinition Height="{StaticResource Top_Part}"/>
<RowDefinition Height="{StaticResource Bottom_Part}"/>
</Grid.RowDefinitions>
Of course, this code is incorrect and doesn't produce desired effect. How can I do this correctly?
The type of the Height property of the RowDefinition is GridLength so you need to create GridLength instances in your resources:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<GridLength x:Key="Top_Part">4*</GridLength>
<GridLength x:Key="Bottom_Part">3*</GridLength >
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="{StaticResource Top_Part}"/>
<RowDefinition Height="{StaticResource Bottom_Part}"/>
</Grid.RowDefinitions>
<Grid Background="Blue" Grid.Row="0"/>
<Grid Background="Red" Grid.Row="1"/>
</Grid>
</Window>
Related
I'm learning about WPF and I'm creating a window in XAML.
The window should look like this:
But when I run the program it looks like this:
The code is the following:
<Page x:Class="WpfApp1.ProductsManagement"
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:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="ProductsManagement">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="300" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="420" />
</Grid.RowDefinitions>
<TextBlock
Margin="5"
Text="Search"
Grid.Row="0"
Grid.Column="0"/>
<TextBox
Margin="5"
Grid.ColumnSpan="2"
Grid.Column="1"
Background ="White"
Grid.Row="0"
Text="hi"/>
<DataGrid
Margin ="5"
Name="dataGrid"
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="1"/>
<Border
Margin ="5"
Grid.Row="1"
Grid.Column="2"/>
</Grid>
</Page>
Any comments or suggestions are welcome.
UPDATE
I'm taking the following code as an example:
<Page x:Class="WpfApp1.Discussion"
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:data="clr-namespace:BikeShop"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Discussion">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<ListBox
Grid.ColumnSpan="2"
Margin="5"/>
<Button
Grid.Row="1"
Grid.Column="1"
Margin="5"
Content="Send" />
<TextBox
Grid.Row="1"
Margin="5"
Text="Type your message here" />
</Grid>
</Page>
And when I run the code it looks like this: (It works correctly)
Your RowDefinitions must be like this:
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
Right now you ask to fill the entire page for the first row and set the second row to a height of 420.
You must define a specific value for the first and * for the second.
You do not see the error in the designer because you set the second row to 420. Obviously you see the first row at 30. But when you go to fullscreen, the first row gets bigger.
Because your Row heights are incorrect. replace your RawDefinitions with:
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
I have two "UserControl" each UserControl have different height.I put this UserControl in the same window, but in different time, the problem is how change height window dynamically according to height UserControl.
First UserControl is:
<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="auto" Width="auto">
<Grid Height="auto" Width="360" ..... >
<Grid VerticalAlignment="Center" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" MinHeight="20.8"/>
<RowDefinition Height="Auto" MinHeight="20"/>
<RowDefinition Height="Auto" MinHeight="18.4"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" MinHeight="20.8"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="33*"/>
<ColumnDefinition Width="17*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="xx" Grid.Column="1" Grid.Row="0" ......./>
<TextBlock Text="yyy" Grid.Column="1" Grid.Row="1" ...... />
............
............
</Grid>
</Grid>
</UserControl>
Secound UserControl is:
<UserControl x:Class="UserControl2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="auto" Width="auto">
<Grid Height="auto" Width="360" ..... >
<Grid VerticalAlignment="Center" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="33*"/>
<ColumnDefinition Width="17*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="xx" Grid.Column="1" Grid.Row="0" ......./>
<TextBlock Text="yyy" Grid.Column="1" Grid.Row="1" />
............
</Grid>
</Grid>
</UserControl>
MainWindow:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MainWindow"
Title="Home" Height="550"*** Width="700" WindowStartupLocation="CenterScreen" ....>
<Grid Name="Move">
<Button Content="xxx" Height="28" TextBlock.FontSize="15" Name="btn1" Click="click1"/>
<Button Content="yyy" Grid.Row="1" Name="btn2" Click="click2"/>
</Grid>
</Window>
In Behind code MainWindow:
private void click1(object sender, RoutedEventArgs e)
{
UserControl1 add = new UserControl1();
Move.Children.Clear();
Move.Children.Add(add);
}
private void click2(object sender, RoutedEventArgs e)
{
UserControl2 add = new UserControl2();
Move.Children.Clear();
Move.Children.Add(add);
}
can you try in code behind do something like:Application.Current.MainWindow.Height = add.Height; And have you tried on main window put the Height auto?
regards,
The Grid at the bottom contains a ListBox. It stretches vertically, but the scrollbar does not appear when it reaches the bottom.
Layout --
<RibbonWindow ResizeMode="CanResize">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel>
<Ribbon ... />
<ListBox
VerticalAlignment="Stretch"
ScrollViewer.VerticalScrollBarVisibility="Auto"
/>
</StackPanel>
</Grid>
</RibbonWindow>
I have heard that StackPanels can cause this behavior, but replacing it with a Grid causes its own set of issues.
EDIT --
This Layout works -
<RibbonWindow ResizeMode="CanResize">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Ribbon Grid.Row="0" />
<ListBox Grid.Row="1"
VerticalAlignment="Stretch"
ScrollViewer.VerticalScrollBarVisibility="Auto"
/>
</Grid>
</RibbonWindow>
Turns out I needed the Grid.Row="x" tags, and then I could remove the StackPanel, and everything worked.
So, I just wanted to understand why this was happening in my WPF app as it seems to be adding a "space" or faint line without me wanting it...
I have the following XAML
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication3.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid x:Name="LayoutRoot" ShowGridLines="False" Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" CornerRadius="10,10,0,0" Height="10" Background="Black"/>
<Border Grid.Row="1" Background="Black">
</Border>
<Border Grid.Row="2" CornerRadius="0,0,10,10" Height="10" Background="Black"/>
</Grid>
<StackPanel Grid.Row="1">
<Button>Some Button</Button>
</StackPanel>
</Grid>
Which renders the following window...
The problem is if you look closely at the last row connector you will see a faint gray line...
If however I replace the <RowDefinition Height="*"/> on the inner grid with a fix pixel size (i.e. <RowDefinition Height="300"/>) the line goes away. Why exactly when I am using the * value does it seem to be adding this "grey line" / "space"?
I think the problem is Anti-Aliasing
Your effect
SnapsToDevicePixels="True"
UseLayoutRounding="False"
RenderOptions.EdgeMode="Aliased"
It looks like layout rounding problem to me. If you are using WPF 4, try setting UseLayoutRounding="true" on your outer grid. Else, take a look at SnapToDevicePixels.
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.uselayoutrounding.aspx
http://www.wpftutorial.net/DrawOnPhysicalDevicePixels.html
I'd like to have a Grid with a row on the top and bottom with either labels or buttons in them. In the middle I plan on using a ListBox. I want the ListBox to expand to use all the available space. It would be nice to not hard code the height of the other two rows. My XAML is below. How can I make the middle section expand automatically? Thanks.
<UserControl x:Class="WpfApplication1.UserControl1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Row="0"
Grid.ColumnSpan="3"
Content="Top Row" />
<ListBox Grid.Row="1"
Grid.ColumnSpan="3" />
<Label Grid.Row="2"
Grid.ColumnSpan="3"
Content="Bottom Row" />
</Grid>
Try setting the middle row to this...
<RowDefinition Height="*" />
Replace the middle
<RowDefinition Height="Auto" />
with
<RowDefinition Height="*" />