XAML window doesn't look like what I want - wpf

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>

Related

Unable to bind to gridview in another usercontrol

I have 3 user controls
One is the parent that holds a GridViewUserControl and a ExportUserControl
The GridViewUserControl and ExportUserControl have their own viewmodels.
When I click the ExportButton in the ExportUserControl I need to pass the gridview to a command in the ExportUserControl. Everything I try is always passing null.
How would I do something like this? ( This is giving the error "binding.elementname cannot be set while using binding.relativesource"
<telerik:RadButton Content="Export"
Command="{Binding ExportCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=view:GridViewUserControl}, ElementName=GridViewData}"
Margin="0,10,0,0" />
EDIT: Parent Control looks like this
<UserControl x:Class="Sample.Sample1.View.ParentUserControl"
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:view="clr-namespace:GroundStation.Configuration.Viewer.View"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
DataContext="{Binding Source={StaticResource Locator}, Path=Main}">
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="150"/>
<ColumnDefinition Width="5" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<view:ExportUserControl Margin="5"
Grid.Column="0"
Grid.Row="2"
Grid.RowSpan="2"
MinWidth="150"
/>
<GridSplitter Grid.Column="1"
Width="5"
Grid.Row="1"
Grid.RowSpan="3"
HorizontalAlignment="Stretch" />
<view:GridViewUserControl
Margin="5"
Grid.Column="2"
Grid.Row="1"
Grid.RowSpan="3"/>
</Grid>
</UserControl>

XmlDataProvider, XPath and ListBox

I've been playing with XamlPad. I thought I'd embed some XML into the XAML to give me a fake set of hierarchical data. I'm not having much joy. This compiles, but doesn't show the items in the list. (Edit: The hierarchical aspect is for later. For now I just want to get stuff appearing in a list).
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Page.Resources>
<XmlDataProvider x:Key="MyXmlData" XPath="ParentNode">
<x:XData>
<MyDoc>Wee
<ParentNode>Hi</ParentNode>
<ParentNode>Low</ParentNode>
</MyDoc>
</x:XData>
</XmlDataProvider>
</Page.Resources>
<Border BorderBrush="Green" BorderThickness="5">
<Grid DataContext="{StaticResource MyXmlData}" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListBox Grid.Row="1" Grid.Column="0" ItemsSource="{Binding}" Background="LightGray">
</ListBox>
</Grid>
</Border>
</Page>
If you remove the XPath="ParentNode" from the XmlDataProvider, it adds the entire document, verbatim, to the listbox. I'd prefer to have two nodes in the Listbox, one for each ParentNode.
Okie dokie, the solution was simple enough, I added an empty namespace to the xml and then did the xpaths as usual. Here's the solution.
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Page.Resources>
<XmlDataProvider x:Key="MyDataProvider" XPath="MyDoc">
<x:XData>
<MyDoc xmlns="">Wee
<ParentNode>Hi</ParentNode>
<ParentNode>Low</ParentNode>
</MyDoc>
</x:XData>
</XmlDataProvider>
</Page.Resources>
<Border BorderBrush="Green" BorderThickness="5">
<Grid DataContext="{StaticResource MyDataProvider}" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListBox Grid.Row="1" Grid.Column="0" ItemsSource="{Binding XPath=*}" Background="LightGray">
</ListBox>
</Grid>
</Border>
</Page>

WPF Alignment not working

I use the following code in my WPF app, for a groupbox:
<GroupBox Style="{StaticResource groupBoxStyle}" Header="RB" Margin="0,6,268,249" Name="groupBoxRB" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="276">
This control is within a grid, that is defined like this:
<TabControl Grid.Row="1" Margin="4,12,0,12" Name="tabControl1" Background="Transparent">
<TabItem Style="{StaticResource tabItemMainStyle}" Header="Main" Name="tabItemMain" >
<Grid Background="Transparent" MinHeight="926" MinWidth="1218">
And that tabcontrol is within the main grid:
<Window x:Class="SRM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:local="clr-namespace:SRM" ResizeMode="CanResize"
Title="SRM"
Width="991" Icon="Resources\Icons\SRM.png"
WindowStartupLocation="CenterScreen" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="1024" Height="774" Visibility="Visible" Foreground="#00000000" Margin="0">
<Grid Style="{StaticResource styleBackground}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2.5" />
</Grid.ColumnDefinitions>
I don't understand why the groupbox i mentioned won't stretch on its vertical axis... any idea?
Thanks.
PS: the staticresources don't define heights/widths/alignments
My problem came from the designers (vs2010's or blend's) that by default put margins if you place controls manually in them... setting the Margin to 0 solved the problem:
<GroupBox Style="{StaticResource groupBoxStyle}" Header="RB" Margin="0" Name="groupBoxRB" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="276">
I think you're missing something in your styles or something. I just made the following from your code and stretching works fine in standalone app.
<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" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2.5" />
</Grid.ColumnDefinitions>
<TabControl Grid.Row="1" Margin="4,12,0,12" Name="tabControl1" Background="Transparent">
<TabItem Header="Main" Name="tabItemMain" >
<Grid Background="Transparent" MinHeight="200" MinWidth="200">
<GroupBox Header="RB" Name="groupBoxRB" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="276">
<Rectangle Fill="Orange" />
</GroupBox>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>

WPF Grid layout panel with row height set to "Auto"

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="*" />

Scrollable Grid in WPF/Silverlight

I would like to build a WPF window which uses an outer Grid to split the screen into 4 parts. In the lower right quadrant, I would like to embed another Grid which is larger than the grid cell. I have been looking for ways to add a ScrollViewer (or use the Grid.ScrollViewer properties) but no matter what I try the inner grid does not resize or display the scrollbars appropriately.
I suspect it has something to do with not wrapping the inner grid with the correct panel with the appropriate sizing (and resizing) behavior which would force the inner grid to honor the scrollbars, instead of simply rendering too big (and being clipped by the other window).
The hosting window is defined like this:
<Window x:Class="GridScrollTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:GridScrollTest"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="OuterGrid">
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<local:SSControl x:Name="Sheet"
Grid.Row="1" Grid.Column="1"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Yellow" />
<Canvas Grid.Row="0" Grid.Column="0" Background="LightGreen" />
<Canvas Grid.Row="1" Grid.Column="0" Background="LightBlue" />
<Canvas Grid.Row="0" Grid.Column="1" Background="LightCoral" />
</Grid>
</Window>
And the referenced SSControl:
<UserControl x:Class="GridScrollTest.SSControl"
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"
Height="270" Width="600">
<ScrollViewer
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Grid x:Name="CellGrid" ShowGridLines="False"
>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
</Grid>
</ScrollViewer>
</UserControl>
I do not know for sure, but after trying your code in Blend, I think your problem might be that you have set the ColumnDefinition.Width and RowDefinition.Height to Auto. Try setting them to * and remove the Height=270 and Width=600 for your user control. This way, the outer grid fills all the available space in the window, and the lower right cell has scroll bars.

Resources