MediaElement layout to occupy entire space - wpf

Below is my xaml code in which I have Mediaelement and 3 button hosted inside grid.
<Grid ShowGridLines="True">
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="Black"
Offset="0" />
<GradientStop Color="#FF4F87F7"
Offset="1" />
</LinearGradientBrush>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="7*" />
<RowDefinition Height="35" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="5*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Border Grid.ColumnSpan="4"
HorizontalAlignment="Stretch"
Grid.Row="1"
BorderThickness="2"
CornerRadius="5"
BorderBrush="Black">
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center">
<Button Width="100"
Content="Previous"
Style="{DynamicResource buttonMargin}" />
<Button Width="100"
Content="Play"
Style="{DynamicResource buttonMargin}"
Click="Button_Click" />
<Button Width="100"
Content="Next"
Style="{DynamicResource buttonMargin}" />
<ToggleButton Style="{DynamicResource toggleButtonMargin}">Full Screen</ToggleButton>
</StackPanel>
</Border>
<MediaElement Name="me"
HorizontalAlignment="Stretch"
LoadedBehavior="Manual"
VerticalAlignment="Top"
Grid.IsSharedSizeScope="True" />
<GridSplitter Grid.Column="1"
Width="3"
HorizontalAlignment="Center" />
<WebBrowser Name="webBrowserScript"
Grid.Column="2" />
</Grid>
Problem: My aim is to render MediaElement in 0th row and 0th column and it should occupy entire space. However, MediaElement is not streaching entirely and not occupying entire space. I tried all possible combinations of HorizontalAlignment and VerticalAlignment. Can anyone help?
Regards,
Hemant

Got it..
Need to use Stretch property of media element.
<MediaElement Name="me" HorizontalAlignment="Stretch" LoadedBehavior="Manual"
VerticalAlignment="Stretch" Stretch="Fill"/>

Related

How to make two child occupy 100% and 20% of parent grid?

I have a grid with 2 rows. and I want to place a rectangle to take 100% of grid height and a text box to be placed outside the grid(overflow).
My XAML Code:
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Height="{Binding Height, ElementName=AppServer}">
<Rectangle.Fill>
<ImageBrush ImageSource="/Images/Server_Base.png"/>
</Rectangle.Fill>
</Rectangle>
<Label ClipToBounds="False" Grid.Row="1" Width="Auto" HorizontalContentAlignment="Right" VerticalContentAlignment="Bottom" Background="Transparent" >
<AccessText TextWrapping="Wrap">App</AccessText>
</Label>
</Grid>
as a simple workaround you may use below
<Grid>
<Rectangle>
<Rectangle.Fill>
<ImageBrush ImageSource="/Images/Server_Base.png" />
</Rectangle.Fill>
</Rectangle>
<Canvas VerticalAlignment="Bottom">
<Label ClipToBounds="False"
Canvas.Right="0"
Width="Auto"
Background="Transparent">
<AccessText TextWrapping="Wrap">App</AccessText>
</Label>
</Canvas>
</Grid>

How to set margin for a single cell inside stack panel for multiple controls in WPF?

I need the textbox1 control ought to put by lable1 and textbox2 control ought to put alongside lable2. I am utilizing single cell from a Grid for this whole control allignment. i can't drag and place the controls wherever i need. is there some other conceivable approach to drag and place my controls inside a cell in grid?
Here's my XAML code..
<Grid Background="#BFBFBF">
<StackPanel Margin="2,2,2,2" >
<Grid Margin="2,2,2,2" VerticalAlignment="Top">
<Grid.RowDefinitions >
<RowDefinition Height="35*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="35*"/>
</Grid.RowDefinitions>
<!-- 1st Content-->
<Border Margin="4,2,2,2" BorderBrush="#FF0100A3" Grid.Row="0" CornerRadius="3,3,3,3" VerticalAlignment="Top" BorderThickness="2,2,2,2" Height="30" MouseLeftButtonDown="brdSectionOne_MouseLeftButtonDown">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF5E68CE" Offset="0"/>
<GradientStop Color="#FF5B64CA" Offset="1"/>
<GradientStop Color="#FF000C82" Offset="0.356"/>
</LinearGradientBrush>
</Border.Background>
<TextBlock Text="Section 1" Grid.ColumnSpan ="2" Foreground="White" Grid.Row="0" FontFamily="Segoe UI" FontSize="13" Margin="8,5,2,0" VerticalAlignment="Top" FontWeight="Normal" Cursor="Hand" />
</Border>
<StackPanel x:Name="spnlFirstSection" Grid.Row="0" VerticalAlignment="Bottom" Height="436" Margin="3,35,3,0" >
<Border Margin="1,1,1,1" CornerRadius="3,3,3,3" VerticalAlignment="Top" BorderThickness="2,2,2,2" Height="430" Background="#FFFFFFFF" BorderBrush="#FF4C4BEE" >
<StackPanel Grid.ColumnSpan ="2" Grid.Row="0" Height="445" Margin="6,6,6,0" VerticalAlignment="Top" >
<Label x:Name="lbl_1" Content="Label 1" Margin="50,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="-1.895,0" FontSize="14"/>
<TextBox x:Name="textbox1" Margin="300,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Text="textbox1" Height="25" TextWrapping="Wrap" Width="250" FontSize="18"/>
<Label x:Name="lbl_2" Content="Label 2" Margin="50,30,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="-1.895,0" FontSize="14"/>
<TextBox x:Name="textbox2" Margin="300,30,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Text="textbox2" Height="25" TextWrapping="Wrap" Width="250" FontSize="18"/>
</StackPanel>
<!--<TextBlock Text="Section 1 Content" Grid.ColumnSpan ="2" Foreground="#FF000000" Grid.Row="0" FontFamily="Segoe UI" FontSize="13" Margin="6,6,6,0" VerticalAlignment="Top" FontWeight="Normal" Cursor="Hand" Height="445" />-->
</Border>
</StackPanel>
</Grid>
</StackPanel>
</Grid>
A StackPanel stacks either Vertically (default) or Horizontally. Like so:
<StackPanel>
<TextBlock Text="Foo"/>
<TextBlock Text="Bar"/>
</StackPanel>
Will render:
Foo
Bar
While
<StackPanel Orientation="Horizontal">
<TextBlock Text="Foo"/>
<TextBlock Text="Bar"/>
</StackPanel>
Will render:
Foo Bar
If you want something to look like a Grid you should use a Grid! So if you want something like this:
LabelOne: Foo
LabelTwo: Bar
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Content="LabelOne:" /> <!-- Grid.Row and Grid.Column default to 0 -->
<Label Content="LabelTwo:" Grid.Row="1" />
<TextBox Text="Foo" Grid.Column="1" />
<TextBox Text="Bar" Grid.Row="1" Grid.Column="1" />
</Grid>
You could also try and nest StackPanels like so:
<StackPanel Orientation="Horizontal">
<StackPanel>
<Label Content="LabelOne:" />
<Label Content="LabelTwo:" />
</StackPanel>
<StackPanel>
<TextBox Text="Foo" />
<TextBox Text="Bar" />
</StackPanel>
</StackPanel>
But that will problably get you some unwanted behaviours.

How to full Screen popup in windows phone 8?

Hi;
i can not fit RadModalWindow on windows phone 8 application. How to fill all screen my popup?
<telerikPrimitives:RadModalWindow Name="cardselect" IsFullScreen="True"
HorizontalAlignment="Center" VerticalAlignment="Center"
PlacementTarget="{Binding ElementName=ContentPanel}"
IsClosedOnOutsideTap="True">
<Grid Name="ModalWindowGrid1"
Width="{Binding ProgramtDetailWidth, Mode=TwoWay}"
Height="{Binding ProgramtDetailHeight, Mode=TwoWay}"
HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.Background>
<ImageBrush ImageSource="Assets/Background.png"/>
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border BorderBrush="#FF0A0611" BorderThickness="0" Grid.Row="2">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF45374D"/>
<GradientStop Color="#FF1E092A" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Border BorderThickness="3" HorizontalAlignment="Left" Width="330" Height="392" VerticalAlignment="Top">
<Border.BorderBrush>
<SolidColorBrush Color="#FFF2F1F3"/>
</Border.BorderBrush>
<StackPanel HorizontalAlignment="Center" Width="330" Margin="-3,0" >
<StackPanel x:Name="MainPanel" Height="385">
<Border BorderThickness="0,0,0,3" Height="70" VerticalAlignment="Top" Margin="0,0,1,0">
<Border.BorderBrush>
<SolidColorBrush Color="#FFF2F1F3"/>
</Border.BorderBrush>
<TextBlock HorizontalAlignment="Center" Margin="0,22,0,15" TextWrapping="Wrap" VerticalAlignment="Center" FontSize="21.333" Height="32" Width="209" Grid.RowSpan="2" Text="Dijital Kart No Seçiniz"/>
</Border>
<StackPanel x:Name="WaitingPanel" Margin="0,60" HorizontalAlignment="Center" Visibility="Collapsed">
<TextBlock Text="Kayıt işlemi sürüyor." VerticalAlignment="Center" FontSize="21.333" HorizontalAlignment="Center"/>
<ProgressBar x:Name="pgb" Width="250" IsIndeterminate="True" Margin="0,10
,0,0" Value="0" />
</StackPanel>
<Grid x:Name="grdClient" HorizontalAlignment="Left" VerticalAlignment="Top" Height="313" Width="329">
<Grid.RowDefinitions>
<RowDefinition Height="255*"/>
<RowDefinition Height="58*"/>
</Grid.RowDefinitions>
<Button x:Name="btnOK" Content="OK" HorizontalAlignment="Right" Height="46" Margin="0,0,21,0" VerticalAlignment="Center" Width="119" Click="btnRecord_Click" Grid.Row="1"/>
<Button x:Name="btnCancel" Content="Vazgeç" HorizontalAlignment="Left" Height="46" Margin="21,0,0,0" VerticalAlignment="Center" Width="119" Click="btnCancel_Click_1" Grid.Row="1"/>
<ListBox Name="lstCards" SelectionMode="Single" HorizontalAlignment="Left" Height="210" Margin="21,21,0,0" VerticalAlignment="Top" Width="287" ItemsSource="{Binding}"></ListBox>
</Grid>
</StackPanel>
</StackPanel>
</Border>
</Border>
</Grid>
</telerikPrimitives:RadModalWindow>
It looks like your window's content is not big enough to fill the screen. The RadModalWindow does fill the whole screen, but any area not covered by your content will show what is behind it via transparency.
Make your main grid auto size to fill the screen. Alternately, setting a background colour or image on your window should ensure the transparency doesn't show the content behind the window.

Assigning border to every Grid row

Currently I am setting the background on each Grid row individually:
<Grid>
<Grid.RowDefinitions><RowDefinition /><RowDefinition /></Grid.RowDefinitions>
<Grid.ColumnDefinitions><ColumnDefinition /><ColumnDefinition /><ColumnDefinition /><ColumnDefinition /><ColumnDefinition /><ColumnDefinition /></Grid.ColumnDefinitions>
<Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="6" Height="24" BorderBrush="#FF252A30" CornerRadius="10" BorderThickness="1">
<Border.Background>
<LinearGradientBrush EndPoint="1.036,0.367" StartPoint="-0.194,0.362">
<GradientStop Color="#AAE098" Offset="0.1"/>
<GradientStop Color="#D5E9D4" Offset="0.9"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<Border Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="6" Height="24" BorderBrush="#FF252A30" CornerRadius="10" BorderThickness="1">
<Border.Background>
<LinearGradientBrush EndPoint="1.036,0.367" StartPoint="-0.194,0.362">
<GradientStop Color="#AAE098" Offset="0.1"/>
<GradientStop Color="#D5E9D4" Offset="0.9"/>
</LinearGradientBrush>
</Border.Background>
</Border>
</Grid>
Surely there must be some way to set this Border once for all rows. How is that done?
Thanks!
Or you could use this grid I just made. It will automatically add a border to every cell in the grid. This is the result:
C#:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace GridWithBorder
{
public class BorderGrid : Grid
{
protected override void OnRender(DrawingContext dc)
{
double leftOffset = 0;
double topOffset = 0;
Pen pen = new Pen(Brushes.Black, 3);
pen.Freeze();
foreach (RowDefinition row in this.RowDefinitions)
{
dc.DrawLine(pen, new Point(0, topOffset), new Point(this.ActualWidth, topOffset));
topOffset += row.ActualHeight;
}
// draw last line at the bottom
dc.DrawLine(pen, new Point(0, topOffset), new Point(this.ActualWidth, topOffset));
//foreach (ColumnDefinition column in this.ColumnDefinitions)
//{
// dc.DrawLine(pen, new Point(leftOffset, 0), new Point(leftOffset, this.ActualHeight));
// leftOffset += column.ActualWidth;
//}
// draw last line on the right
//dc.DrawLine(pen, new Point(leftOffset, 0), new Point(leftOffset, this.ActualHeight));
base.OnRender(dc);
}
}
}
XAML:
<Window x:Class="GridWithBorder.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:GridWithBorder"
Title="MainWindow" Height="350" Width="525">
<local:BorderGrid>
<local:BorderGrid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</local:BorderGrid.RowDefinitions>
<local:BorderGrid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</local:BorderGrid.ColumnDefinitions>
<Rectangle Grid.Row="0" Grid.Column="0" Fill="Red" Margin="5" />
<Rectangle Grid.Row="0" Grid.Column="1" Fill="Blue" Margin="5" />
<Rectangle Grid.Row="0" Grid.Column="2" Fill="Orange" Margin="5" />
<Rectangle Grid.Row="0" Grid.Column="3" Fill="Red" Margin="5" />
<Rectangle Grid.Row="1" Grid.Column="0" Fill="Yellow" Margin="5" />
<Rectangle Grid.Row="1" Grid.Column="1" Fill="Green" Margin="5" />
<Rectangle Grid.Row="1" Grid.Column="2" Fill="Purple" Margin="5" />
<Rectangle Grid.Row="1" Grid.Column="3" Fill="Green" Margin="5" />
<Rectangle Grid.Row="2" Grid.Column="0" Fill="Orange" Margin="5" />
<Rectangle Grid.Row="2" Grid.Column="1" Fill="Red" Margin="5" />
<Rectangle Grid.Row="2" Grid.Column="2" Fill="Blue" Margin="5" />
<Rectangle Grid.Row="2" Grid.Column="3" Fill="Red" Margin="5" />
</local:BorderGrid>
</Window>
You could pull that border out into a reusable resource, but I suspect what you're really trying to do is create a GridView.
You can just set the Background property on your Grid. If there is commonality between the border which you are applying to the different rows, you can create a default style (and if desired, limit the scope of this style to the Grid itself):
XAML
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type Border}">
<!-- All rows -->
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="CornerRadius" Value="5" />
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Border Grid.Row="0">
<TextBlock Text="This row has a black border (default)." />
</Border>
<Border BorderBrush="Red" Grid.Row="1">
<TextBlock Text="This row has a red border." />
</Border>
<Border BorderBrush="Green" BorderThickness="4" Grid.Row="2">
<TextBlock Text="This has a thick green border." />
</Border>
</Grid>
With a default Style, no additional property needs to be set on your row's Border to achieve a default look (row one above). If a certain row needs to tweak the look and feel, then just provide additional properties on the Border to override the ones set in the default Style (rows two and three above). If this technique is something you are applying across multiple views in your application, then you can extract this style into a separate ResourceDictionary and simply merge it where appropriate.
Hope this helps!

WPF TexBlock in Grid in ListView not Sizing Correctly?

I have a TextBlock that is in a Grid that is an ItemTemplate for a ListView. I have the items so that they grow when the window is resized, but I cannot figure out how to have the TextBlock be limited to that size. I've tried to do this with the width on the ColumnDefinition - if I set the Width to a fixed number (say 350) the text wraps correctly, but obviously the TextBlock doesn't expand when the window is expanded - if I set the Width to "*" the there is then a horizontal scroll bar and the text runs off to the right and doesn't wrap.
Any idea what I'm doing wrong here?
<GroupBox Header="Urgent Items" Margin="8,8,8,340" Name="UrgetItemsGroupBox">
<Grid>
<ListView Margin="6" Name="CriticalErrorsListView" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Path=.}" MouseDoubleClick="CriticalErrorsListView_MouseDoubleClick">
<ListView.Background>
<LinearGradientBrush EndPoint="-0.192,0.529" StartPoint="0.998,0.519">
<GradientStop Color="#FFD2D2D2" Offset="0"/>
<GradientStop Color="#FFFFFFFF" Offset="1"/>
</LinearGradientBrush>
</ListView.Background>
<ListView.ItemTemplate>
<DataTemplate>
<Border Margin="2,2,2,3" BorderBrush="#FF847F6E" CornerRadius="10" BorderThickness="3">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25" />
<RowDefinition Height="20" />
<RowDefinition Height="20" />
<RowDefinition Height="75" />
</Grid.RowDefinitions>
<Image Grid.Row="0" Grid.RowSpan="5" Grid.Column="0" Margin="2,2,2,2" Source="Images\errorIcon.png" />
<TextBlock Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="2" Margin="2,2,2,2" Text="{Binding Path=ApplicationName}" FontFamily="Calibri" FontWeight="Bold" FontSize="18" />
<TextBlock Grid.Row="1" Grid.Column="2" Margin="10,2,10,2" Text="{Binding Path=ErrorTime}" FontFamily="Calibri" FontSize="12" />
<TextBlock Grid.Row="2" Grid.Column="2" Margin="10,2,10,2" Text="{Binding Path=ErrorPerson}" FontFamily="Calibri" FontSize="12" />
<TextBlock Grid.Row="3" Grid.Column="2" Margin="2,2,2,2" Text="{Binding Path=ShortDescription}" TextWrapping="Wrap" />
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</GroupBox>
You see horizontal scroll bar because ListView uses ScrollViewer in its template to allow scrolling. All you need to do is say ScrollViewer to not scroll horizontally. Just set ScrollViewer.HorizontalScrollBarVisibility="Disabled" on your ListView. So you'll have something like this:
<ListView Margin="6"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Name="CriticalErrorsListView"
HorizontalContentAlignment="Stretch"
ItemsSource="{Binding Path=.}"
MouseDoubleClick="CriticalErrorsListView_MouseDoubleClick">
<!-- The rest goes here. -->
</ListView>
Hope this helps.

Resources