Silverlight - Layout Question - silverlight

I am creating a Silverlight application that should take up the width of the user's screen. This application has a horizontal row that greets the user. Then, two columns below it. The right column is always a fixed size. I want the left column to take up any remaining space. In an attempt to accomplish this, I am using the following XAML:
<Grid x:Name="layoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid x:Name="greetingGrid" Margin="0,0,0,8">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Welcome " />
<TextBlock x:Name="usernameTextBlock" />
</StackPanel>
</Grid>
<Grid x:Name="contentGrid" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid x:Name="leftGrid" HorizontalAlignment="Stretch">
<Border x:Name="leftBorder" BorderBrush="Black" Height="250">
<!-- Insert Wrap Panel of Images --!>
</Border>
</Grid>
<Grid x:Name="rightGrid" Width="100" Grid.Column="1" HorizontalAlignment="Right" Margin="8,0,0,0">
<Border BorderBrush="Black" BorderThickness="2">
<StackPanel Orientation="Vertical">
<TextBlock Text="How would you like to view the images?" />
<ComboBox x:Name="optionComboBox">
<ComboBoxItem Content="Name" />
<ComboBoxItem Content="Date" />
</ComboBox>
</StackPanel>
</Border>
</Grid>
</Grid>
</Grid>
Oddly, the two lower columns are split evenly in size. How do I make the left column take up all remaining space?
Thank you!

<Grid.ColumnDefinitions>
<ColumnDefinition width="*" />
<ColumnDefinition width="250"/>
</Grid.ColumnDefinitions>
Use * for the grid column to take the rest of the available space. Keep in mind that the parent container also needs to take the whole area to make it work!

You can specify the widths of the columns rather than on your "rightGrid", eg:
<Grid x:Name="greetingGrid" Margin="0,0,0,8">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Welcome " />
<TextBlock x:Name="usernameTextBlock" />
</StackPanel>
</Grid>
<Grid x:Name="contentGrid" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100px" />
</Grid.ColumnDefinitions>
<Grid x:Name="leftGrid" HorizontalAlignment="Stretch" Background="Fuchsia">
<Border x:Name="leftBorder" BorderBrush="Black" Height="250">
</Border>
</Grid>
<Grid x:Name="rightGrid" Grid.Column="1" HorizontalAlignment="Right" Margin="8,0,0,0">
<Border BorderBrush="Black" BorderThickness="2">
<StackPanel Orientation="Vertical">
<TextBlock Text="How would you like to view the images?" />
<ComboBox x:Name="optionComboBox">
<ComboBoxItem Content="Name" />
<ComboBoxItem Content="Date" />
</ComboBox>
</StackPanel>
</Border>
</Grid>

Related

Textbox doesn't fit to its content size in an expander

I have a Xaml view where I'm trying to display Textbox inside a grid which is inside an Exander.
<Expander DataContext="{Binding DiagnosticCategories[0].DiagnosticResults[0]}" <!-- For the test -->
Background="Transparent"
Foreground="{StaticResource ActiveForegroundBrush}"
IsExpanded="False">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<StackPanel
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Top">
<TextBox
Margin="10"
Background="Transparent"
BorderThickness="0"
FontSize="13"
FontWeight="Light"
Foreground="{StaticResource ActiveForegroundBrush}"
IsReadOnly="True"
Opacity="0.8"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectionBrush="Black"
Text="{Binding FormatedParameters, Mode=OneWay}"
TextWrapping="Wrap" />
</StackPanel>
[...]
</Grid>
</Expander>
However, there is a problem with the Textbox which has a anormal height even if my text is just "aa"...
First, I thought that the problem was with the Grid.Row and the Textbox only fit it so I tried to add a StackPanel which doesn't fit the Grid.Row but it doesn't work. It seems that the problem is in the textbox.
With a TextBlock, I don't have this problem but I need the Textbox to display my text.
You can try RichTextBox.
When I want to use TextBox, I have the same problem under a certain height. I solved the problem by using RichTextBox instead of TextBox.
<Expander DataContext="{Binding DiagnosticCategories[0].DiagnosticResults[0]}" <!-- For the test -->
Background="Transparent"
Foreground="{StaticResource ActiveForegroundBrush}"
IsExpanded="False">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<StackPanel
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Top">
<RichTextBox
Margin="10"
Background="Transparent"
BorderThickness="0"
FontSize="13"
FontWeight="Light"
Foreground="{StaticResource ActiveForegroundBrush}"
IsReadOnly="True"
Opacity="0.8"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectionBrush="Black"
Text="{Binding FormatedParameters, Mode=OneWay}"
TextWrapping="Wrap" />
</StackPanel>
[...]
</Grid>
</Expander>
As others mentioned, I'm a little unsure what you are trying to achieve. I added a couple of horizontal and vertical layouts to your example:
<Expander DataContext="{Binding DiagnosticCategories[0].DiagnosticResults[0]}"
Background="Yellow" HorizontalAlignment="Center" VerticalAlignment="Center" MinWidth="200"
IsExpanded="False">
<Grid Margin="10" Background="Blue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<StackPanel
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Top">
<TextBox
Margin="10"
Background="Orange"
BorderThickness="0"
FontSize="13"
FontWeight="Light"
IsReadOnly="True"
Opacity="0.8"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectionBrush="Black"
Text="Test Text"
TextWrapping="Wrap"
/>
</StackPanel>
</Grid>
</Expander>
And I get the following:
Closed:
Open:

WPF, xaml grid with TabControl

I have an application (implemenented in WPF, XAML) which has 6 rows and 6 columns. At the position Grid.Row="1" Grid.RowSpan="4" Grid.Column="0" Grid.ColumnSpan="5" , I'm using a TabControl for Dashboard, Housekeeping, and Science. In every TabControl, I'm using a ListBox on the left side to choose individual data. The rest (w/o the ListBox) of the field/grid (which should have a size of 4 rows and 4 columns) is available for charts.
Problem
The charts which are visible in the screenshot below should be stretched over 4 rows down to the bottom edge of the application according to the XAML code. The TabControl is not 4 rows as visible because of the white boarder I have attached.
How can I change my grid-concept that it works as described above? Is a grid in the grid, resp. in the TabControl necessary at all?
Screenshot of the WPF application
MainWindow.xaml
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<!-- Definition of Rows and Columns -->
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="250" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="250" />
</Grid.ColumnDefinitions>
<!-- Header -->
<StackPanel Grid.Row="0" Grid.ColumnSpan="6">
<TextBlock Text="RFM DATA ANALYZER" Margin="20" HorizontalAlignment="Center" FontSize="36" FontWeight="Thin" />
</StackPanel>
<!-- Tabs -->
<StackPanel Grid.Row="1" Grid.RowSpan="4" Grid.Column="0" Grid.ColumnSpan="5">
<TabControl TabStripPlacement="Top">
<TabItem Header="Dashboard">
<Border BorderBrush="White" BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.RowSpan="4" Grid.Column="0" Grid.ColumnSpan="1">
<TextBlock Text="Data" Margin="0 50 0 20" HorizontalAlignment="Left" FontFamily="Segoe UI Semibold" FontSize="15" Foreground="LightGray" />
<ListBox >
<ListBoxItem Content="Data 00" />
<ListBoxItem Content="Data 01" />
<ListBoxItem Content="Data 03" />
</ListBox>
</StackPanel>
<oxy:PlotView Model="{Binding Model1}" Background="Transparent" Grid.Row="0" Grid.RowSpan="4" Grid.Column="1" Grid.ColumnSpan="1" />
<oxy:PlotView Model="{Binding Model2}" Background="Transparent" Grid.Row="0" Grid.RowSpan="4" Grid.Column="2" Grid.ColumnSpan="1" />
<oxy:PlotView Model="{Binding Model3}" Background="Transparent" Grid.Row="0" Grid.RowSpan="4" Grid.Column="3" Grid.ColumnSpan="1" />
<oxy:PlotView Model="{Binding Model4}" Background="Transparent" Grid.Row="0" Grid.RowSpan="4" Grid.Column="4" Grid.ColumnSpan="1" />
</Grid>
</Border>
</TabItem>
<TabItem Header="Housekeeping" Height="40">
<Border BorderBrush="White" BorderThickness="1">
<Grid>
<Grid.ColumnDefinitions>
etc.
Replace your StackPanels with Grids. StackPanels size to their child elements, whereas Grids size to their parent.
<StackPanel Grid.Row="1" Grid.RowSpan="4" Grid.Column="0" Grid.ColumnSpan="5">
<TabControl TabStripPlacement="Top">
...becomes...
<Grid Grid.Row="1" Grid.RowSpan="4" Grid.Column="0" Grid.ColumnSpan="5">
<TabControl TabStripPlacement="Top">

Settings Visibility of Grid Hides Another Grid

I have two grid controls sitting within the same row of a parent grid.
<Grid x:Name="grdTimelinePlusControls" Grid.Row="1" Grid.ColumnSpan="3">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ProgressBar x:Name="pgbVideoTimeline" HorizontalAlignment="Stretch" Height="7" Background="{StaticResource SecondaryColour}" Foreground="Maroon" BorderThickness="0" Style="{StaticResource ExpandContractTimeline}" Grid.Row="1" MouseDown="pgbVideoTimeline_MouseDown" MouseMove="pgbVideoTimeline_MouseMove" MouseUp="pgbVideoTimeline_MouseUp" />
<Grid x:Name="grdControls" Grid.Row="2" Background="{StaticResource BackgroundColour}" Height="40" Style="{StaticResource ExpandHideControls}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel x:Name="stpPlaybackControls" Orientation="Horizontal" HorizontalAlignment="Left" Grid.Column="0">
<Image x:Name="btnPlayPause" Height="30" Margin="10,5,5,5" ToolTip="Play" Source="Resources/Images/UI/Play.png" MouseEnter="buttonHover_MouseEnter" MouseLeave="buttonHover_MouseLeave" MouseUp="btnPlayPause_MouseUp" />
<Image x:Name="btnReplay" Height="30" Margin="5" ToolTip="Replay" Source="Resources/Images/UI/Replay.png" MouseEnter="buttonHover_MouseEnter" MouseLeave="buttonHover_MouseLeave" MouseUp="btnReplay_MouseUp" />
</StackPanel>
<StackPanel x:Name="stpMiscControls" Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="1">
<Slider x:Name="slrSpeed" Width="100" Margin="5,10,5,5" VerticalAlignment="Top" Minimum="1" Maximum="5" Value="3" LargeChange="1" IsSnapToTickEnabled="True" ValueChanged="slrSpeed_ValueChanged" />
<Image x:Name="btnOpen" Height="30" Margin="5" ToolTip="Open" Source="Resources/Images/UI/Open.png" MouseEnter="buttonHover_MouseEnter" MouseLeave="buttonHover_MouseLeave" />
<Image x:Name="btnFullScreen" Height="30" Margin="5" ToolTip="Fullscreen" Source="Resources/Images/UI/FullScreen.png" MouseEnter="buttonHover_MouseEnter" MouseLeave="buttonHover_MouseLeave" MouseUp="btnFullScreen_MouseUp" />
<Image x:Name="btnSettings" Height="30" Margin="5,5,10,5" ToolTip="Settings" Source="Resources/Images/UI/Settings.png" MouseEnter="buttonHover_MouseEnter" MouseLeave="buttonHover_MouseLeave" />
</StackPanel>
</Grid>
</Grid>
<Grid x:Name="grdWorkingTime" Grid.Row="1" Grid.ColumnSpan="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ProgressBar x:Name="pgbWorkingTime" Background="{StaticResource BackgroundColour}" BorderBrush="{StaticResource ForegroundColour}" Width=500 Foreground="Maroon" Margin="5" />
</Grid>
I only intend to show one of grdTimelinePlusControls and grdWorkingTime at a time and with the code as it is above I see both a progressbar (pgbWorkingTime) over the top of the various images I have on the two StackPanels. However when I change the opening tag of grdTimelinePlusControls to be:
<Grid x:Name="grdTimelinePlusControls" Grid.Row="1" Grid.ColumnSpan="3" Visibility="Collapsed">
It stops either grid from showing and I can't understand why. It seems like grdWorkingTime must be a child of grdTimelinePlusControls somehow but I cant see it, the document outline also shows them at the same level.
This is obviously not all of the code on my page but I wanted to keep it readable... The full page is here
Is there any particular reason that the two grids are in the same parent grid's row? If you place them in two separate rows with <RowDefinition Height="Auto" /> the 'unused' row should collapse together with the corresponding child grid as you don't intend to show them together at the same time.

Control as background of Grid

I have a grid, and I need to have a control as background of this grid. This control will be a progressbar, but it's my problem how to create it.
I can't find how can I set a control as background of grid.
Have you got any experience with this kind of problem?
<DataTemplate x:Key="TodoItemWeeklyTemplate">
<Grid MinWidth="800" Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Image Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" Source="{Binding Occurrence.Appointment.Icon, Converter={StaticResource imgConverter}}" Width="16" Height="16" Stretch="Fill" />
<TextBlock Grid.Column="1" HorizontalAlignment="Left" MaxWidth="130" Text="{Binding Occurrence.Appointment.Subject}" />
</Grid>
</DataTemplate>
Here is how I would do it:-
<Grid MinWidth="800">
<Rectangle x:Name="Background" Fill="Green" Width="{Binding PercentDone, Converter={StaticConverter WidthConv}, ConverterParameter=800}" HorizontalAlignment="Left" />
<Grid Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Image Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" Source="{Binding Occurrence.Appointment.Icon, Converter={StaticResource imgConverter}}" Width="16" Height="16" Stretch="Fill" />
<TextBlock Grid.Column="1" HorizontalAlignment="Left" MaxWidth="130" Text="{Binding Occurrence.Appointment.Subject}" />
</Grid>
</Grid>
Where WidthConv is an instance of a class added to the usercontrol resources that implements IValueConverter. The Convert method would take the input percentage value and apply it to the parameter to return the width the rectangle needs to be to represents that percentage value.
The important point here is that Grid naturaly overlays elements on each other when the occupy the same area.
You cannot set a control as a background as it is of Brush Type.
But if you want to emulate that you can put your Grid in another one like that :
<Grid>
<Grid>
<!-- put your Content here -->
</Grid>
<ProgressBar />
</Grid>

WPF Have controls fill entire row

I am trying to get 2 buttons to be beside each other and evenly take up the entire row underneath the "main area".
I seem to be missing something
Thanks!
<Grid Background="Transparent" Margin="0" Opacity=".8">
<Border Name="MaskBorder" Grid.RowSpan="3">
<Border Name="MainBorder" Background="Aqua">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="811*"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<!--Body-->
<Grid Grid.ColumnSpan="4">
<Border Name="BodyBackground" Background="White" />
</Grid>
<!-- Main Area for Content -->
<Grid Name="ContentGrid" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="55" />
<RowDefinition Height="811*" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<DockPanel LastChildFill="True" Grid.Row="1">
<Grid DockPanel.Dock="Bottom">
<Button Height="55"></Button>
<Button Height="55"></Button>
</Grid>
<Border Background="Black" />
</DockPanel>
</Grid>
</Grid>
</Border>
</Border>
</Grid>
You have the buttons on the same grid cell. Try:
<Grid DockPanel.Dock="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Height="55"></Button>
<Button Grid.Column="1" Height="55"></Button>
</Grid>
you can use StackPanel Instead of DockPanle like this:
<StackPanel Orientation="Vertical">
<Button Height="55"/>
<Button Height="55"/>
</StackPanel>

Resources