The ScrollViewer does not scroll - c#

I would like to have an interface with 3 components one next to the other.
The first would be a ListView and the two others being Grids.
Since the components will overflow on the right, I want to put them in a ScrollViewer.
I did not succeed. I tried to do a really simple example to try, but even the example fails.
<ScrollViewer Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="600" Height="400">
<StackPanel Width="1200" Height="400" Orientation="Horizontal">
<Border Background="AntiqueWhite" Width="400" Height="400" HorizontalAlignment="Left" VerticalAlignment="Top" />
<Border Background="Blue" Width="400" Height="400" HorizontalAlignment="Left" VerticalAlignment="Top" />
<Border Background="LimeGreen" Width="400" Height="400" HorizontalAlignment="Left" VerticalAlignment="Top" />
</StackPanel>
</ScrollViewer>
As you can see, the ScrollViewer is Inside a Grid.
What did I miss?

Try settings these properties on the scroll viewer:-
<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" ZoomMode="Disabled" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="600" Height="400">
<StackPanel Width="1200" Height="400" Orientation="Horizontal">
<Border Background="AntiqueWhite" Width="400" Height="400" HorizontalAlignment="Left" VerticalAlignment="Top" />
<Border Background="Blue" Width="400" Height="400" HorizontalAlignment="Left" VerticalAlignment="Top" />
<Border Background="LimeGreen" Width="400" Height="400" HorizontalAlignment="Left" VerticalAlignment="Top" />
</StackPanel>
</ScrollViewer>
That works for me normally!

Related

wpf-Xaml line is not showing up while running the code

I have started learning wpf. I am trying to use line element but whenever i try to run this code line is not visible at all. Same case with rectangle, I have used rectangle element as well but it is also not showing up. What is the problem? I don't understand. I am able to see that in designer view but not in the runtime.
<Window x:Class="Mi_Express.Product_Details"
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:Mi_Express"
mc:Ignorable="d"
Title="Product_Details" Height="1920" Width="1080" WindowState="Maximized" ResizeMode="NoResize" WindowStyle="None"
Foreground="White" FontSize="30" FontWeight="Bold" Topmost="True" Background="White">
<Grid>
<Line HorizontalAlignment="Stretch" X1="10" Y1="10" X2="870" Y2="10" Stroke="Black" StrokeThickness="4" Margin="120,180,79,1712"/>
<Label Content="Shopping Cart:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="157,125,0,0" FontSize="30" FontWeight="DemiBold"/>
<Rectangle HorizontalAlignment="center" Height="73" Stroke="Black" StrokeThickness="3" VerticalAlignment="Top" Width="1017" Margin="28,1663,35,0"
/>
<Image HorizontalAlignment="Left" Height="102" VerticalAlignment="Top" Width="139" Margin="157,270,0,0"/>
<Button Content="Go Back" ClickMode="Press" Background="#FFE87E04" BorderBrush="#e87e04" FontSize="18" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Top" Width="174" Margin="157,1786,0,0" Height="61"/>
<Button Content="Continue" ClickMode="Press" Background="#e87e04" BorderBrush="#e87e04" FontSize="18" Foreground="white" HorizontalAlignment="Right" VerticalAlignment="Top" Width="160" Margin="0,1786,159,0" Height="61"/>
</Grid>
</Window>
The problem here is with the structure of your grid ..Dont apply margin to any of the UI elements over here.. instead remove it and use grid.column to position your UI elements..I have modified your code a litte
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Line Grid.Column="0" HorizontalAlignment="Stretch" X1="10" Y1="10" X2="870" Visibility="Visible" Y2="10" Stroke="Red" StrokeThickness="4" Margin="12,18,79,171"/>
<Label Grid.Column="1" Content="Shopping Cart:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="157,125,0,0" FontSize="30" FontWeight="DemiBold"/>
<Rectangle Grid.Column="2"
Visibility="Visible"
Fill="Blue"
HorizontalAlignment="center" Height="73" Stroke="Black" StrokeThickness="3" VerticalAlignment="Top" Width="1017" Margin="28,166,35,0"
/>
<Image HorizontalAlignment="Left" Height="102" VerticalAlignment="Top" Width="139" Margin="157,270,0,0"/>
<Button Content="Go Back" ClickMode="Press" Background="#FFE87E04" BorderBrush="#e87e04" FontSize="18" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Top" Width="174" Margin="157,1786,0,0" Height="61"/>
<Button Content="Continue" ClickMode="Press" Background="#e87e04" BorderBrush="#e87e04" FontSize="18" Foreground="white" HorizontalAlignment="Right" VerticalAlignment="Top" Width="160" Margin="0,1786,159,0" Height="61"/>
</Grid>
For more information on how to use grid in wpf .. please follow this links:
1) http://www.c-sharpcorner.com/UploadFile/1e050f/grid-layout-in-wpf/
2) http://www.wpf-tutorial.com/panels/grid-rows-and-columns/
3) https://wpftutorial.net/GridLayout.html
The problem is in the margin. Bottom margin is way too high and line goes out of the window
I guess you mistaken height with width and you ment Width="1920" Height="1080". Even so the actually height of the grid it would not be 1080, you can check it using this line
<Label Content="{Binding ElementName=grid,Path=ActualHeight}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="157,175,0,0" FontSize="30" FontWeight="DemiBold"/>

I want make Dynamic window content(img&font&frame) size in WPF at c#

enter image description here
enter image description here
I want to fill the monitor's full screen with 1024*768 ratio of window size& content size.
When use at wide monitor's Fill Blank Right Left Area fill black color whit use x:Name="wide_Out"
how can I make dynamic Full screan window & contents (keep main win size rate), regardless of monitor resolution and type.
mainwindow.xaml
<Window x:Class="C.MainWindow"
xmlns:local="clr-namespace:M_C"
mc:Ignorable="d"
Title="MainWindow" Height="768" Width="1024" WindowStyle="None">
<Grid x:Name="wide_Out" Margin="0,0,0,0">
<Grid Height="768" Width="1024">
<DockPanel x:Name="L_black" HorizontalAlignment="Left" Height="737"
LastChildFill="False" VerticalAlignment="Top" Width="62"
Background="#FF242424">
<Button Margin="15,8,0,0" Height="40" VerticalAlignment="Top"
Width="30"/>
</DockPanel>
<DockPanel x:Name="T_blue" HorizontalAlignment="Left" Height="100"
LastChildFill="False" Margin="62,0,0,0" VerticalAlignment="Top"
Width="954" Background="#FF248BC7">
<TextBlock Margin="200,10,200,0" Height="80"
TextWrapping="Wrap"
Text="TextBlock" VerticalAlignment="Top" Width="554"/>
</DockPanel>
<DockPanel x:Name="L_blue" HorizontalAlignment="Left" Height="637"
LastChildFill="False" Margin="62,100,0,0" VerticalAlignment="Top"
Width="82" Background="#FF248BC7"/>
<DockPanel x:Name="R_blue" HorizontalAlignment="Left" Height="637"
LastChildFill="False" Margin="934,100,0,0" Background="#FF248BC7"
Width="82"/>
<DockPanel x:Name="B_blue" HorizontalAlignment="Left" Height="100"
LastChildFill="False" Margin="144,637,0,0" VerticalAlignment="Top"
Width="790" Background="#FF248BC7">
<Image Margin="250,15,250,15" Height="70"
VerticalAlignment="Top" Width="290" />
</DockPanel>
<DockPanel x:Name="main_content_panel" HorizontalAlignment="Left"
Height="537" LastChildFill="False" Margin="144,100,0,0"
VerticalAlignment="Top" Width="790">
<Grid Margin="0,0,0,0">
</Grid>
</DockPanel>
</Grid>
</Grid>
</Window>
If I understand you correctly, what you need to do is wrap the content of your Window element with a Viewbox. Here is sample code, from every element I have I have only written the parts that are important, other may be set as needed:
<Window
Height="{x:Static SystemParameters.PrimaryScreenHeight}"
Width="{x:Static SystemParameters.PrimaryScreenWidth}" <!--To set your window size to the size of monitor-->
WindowStyle="None" <!--To not display any controls-->
>
<Viewbox> <!--You can try using different 'Stretch' attribute values-->
<Grid Width="768" Height="1024" x:Name="wide_Out"> <!--Or whatever dimensions you want your 'base' window to work with-->
<DockPanel x:Name="L_black" HorizontalAlignment="Left" Height="737" LastChildFill="False" VerticalAlignment="Top" Width="62" Background="#FF242424">
<Button Margin="15,8,0,0" Height="40" VerticalAlignment="Top" Width="30"/>
</DockPanel>
<DockPanel x:Name="T_blue" HorizontalAlignment="Left" Height="100" LastChildFill="False" Margin="62,0,0,0" VerticalAlignment="Top" Width="954" Background="#FF248BC7">
<TextBlock Margin="200,10,200,0" Height="80" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="554"/>
</DockPanel>
<DockPanel x:Name="L_blue" HorizontalAlignment="Left" Height="637" LastChildFill="False" Margin="62,100,0,0" VerticalAlignment="Top" Width="82" Background="#FF248BC7"/>
<DockPanel x:Name="R_blue" HorizontalAlignment="Left" Height="637" LastChildFill="False" Margin="934,100,0,0" Background="#FF248BC7" Width="82"/>
<DockPanel x:Name="B_blue" HorizontalAlignment="Left" Height="100" LastChildFill="False" Margin="144,637,0,0" VerticalAlignment="Top" Width="790" Background="#FF248BC7">
<Image Margin="250,15,250,15" Height="70" VerticalAlignment="Top" Width="290" />
</DockPanel>
<DockPanel x:Name="main_content_panel" HorizontalAlignment="Left" Height="537" LastChildFill="False" Margin="144,100,0,0" VerticalAlignment="Top" Width="790">
<Grid Margin="0,0,0,0">
</Grid>
</DockPanel>
</Grid>
</Viewbox>
</Window>
The important parts are the window width and height, the viewbox element and then the actual width and height of the wide_Out Grid, with which you will work when setting inner dimensions and margins.
If you want to, you can use BackgroundColorattribute of the window to set background color for the area that is not covered by your content when screen's aspect ratio is other than 4:3 (or whatever you set in `wide_Out Grid).

Horizontal scroll for stackpanel doesn't work

I try to create a scrollable horizontal StackPanel but I do not succeed very well...
Currently I have my StackPanel with an auto width (and the problem is maybe here) that contains some items like grids.
Now, if all my grids are not visibles in the StackPanel (width is too short) I can't scroll.
I already tried to put the StackPanel within a ScrollViewer but it doesn'
t work too.
How can I fix this?
EDIT here is my code:
<StackPanel Height="85" Margin="0,0,200,15" VerticalAlignment="Bottom">
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Height="85" CanContentScroll="True">
<StackPanel x:Name="Film" Height="85" Width="Auto" Margin="0,0,0,0" Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Visible" CanHorizontallyScroll="True" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True" d:LayoutOverrides="TopPosition, BottomPosition">
<StackPanel.Background>
<SolidColorBrush Color="{DynamicResource ButtonBackground}"/>
</StackPanel.Background>
<Grid Width="100" Background="Red"/>
<Grid Width="100" Background="#FFFF0051"/>
<Grid Width="100" Background="#FFB900FF"/>
<Grid Width="100" Background="#FF002EFF"/>
<Grid Width="100" Background="#FF00FFDC"/>
<Grid Width="100" Background="#FF51FF00"/>
<Grid Width="100" Background="Red"/>
</StackPanel>
</ScrollViewer>
</StackPanel>
Currently I have my stackpanel with an auto width (and the problem is maybe here) that contains some items like grids.
This is your problem. A StackPanel measures its children with infinite horizontal space if its Orientation property is set to Horizontal and infinite vertical space if it is set to Vertical. So you will have to specify an explicit width for the StackPanel itself or the ScrollViewer for this to work.
Alternatively you could put the ScrollViewer in a Panel that measures its children, like for example a Grid (but not a StackPanel). This works for example:
<Window x:Class="WpfApplication1.Window1"
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:WpfApplication1"
mc:Ignorable="d"
Title="Window" Height="300" Width="300">
<Grid>
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Height="85" CanContentScroll="True">
<StackPanel x:Name="Film" Height="85" Width="Auto" Margin="0,0,0,0" Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Visible" CanHorizontallyScroll="True" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True" d:LayoutOverrides="TopPosition, BottomPosition">
<StackPanel.Background>
<SolidColorBrush Color="Yellow"/>
</StackPanel.Background>
<Grid Width="100" Background="Red"/>
<Grid Width="100" Background="#FFFF0051"/>
<Grid Width="100" Background="#FFB900FF"/>
<Grid Width="100" Background="#FF002EFF"/>
<Grid Width="100" Background="#FF00FFDC"/>
<Grid Width="100" Background="#FF51FF00"/>
<Grid Width="100" Background="Red"/>
</StackPanel>
</ScrollViewer>
</Grid>
</Window>
But this doesn't because the StackPanel is considered to have an infinite width:
<StackPanel>
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Height="85" CanContentScroll="True">
<StackPanel x:Name="Film" Height="85" Width="Auto" Margin="0,0,0,0" Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Visible" CanHorizontallyScroll="True" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True" d:LayoutOverrides="TopPosition, BottomPosition">
<StackPanel.Background>
<SolidColorBrush Color="Yellow"/>
</StackPanel.Background>
<Grid Width="100" Background="Red"/>
<Grid Width="100" Background="#FFFF0051"/>
<Grid Width="100" Background="#FFB900FF"/>
<Grid Width="100" Background="#FF002EFF"/>
<Grid Width="100" Background="#FF00FFDC"/>
<Grid Width="100" Background="#FF51FF00"/>
<Grid Width="100" Background="Red"/>
</StackPanel>
</ScrollViewer>
</StackPanel>
Putting ScrollViewers inside StackPanels is a bad idea.
You should put your StackPanel into a ScrollViewer like this:
<ScrollViewer Height="85" VerticalAlignment="Bottom" Margin="0,0,200,15" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto">
<StackPanel x:Name="Film" Orientation="Horizontal" >
<StackPanel.Background>
<SolidColorBrush Color="Black"/>
</StackPanel.Background>
<Grid Width="100" Background="Red"/>
<Grid Width="100" Background="#FFFF0051"/>
<Grid Width="100" Background="#FFB900FF"/>
<Grid Width="100" Background="#FF002EFF"/>
<Grid Width="100" Background="#FF00FFDC"/>
<Grid Width="100" Background="#FF51FF00"/>
<Grid Width="100" Background="Red"/>
</StackPanel>
</ScrollViewer>

UWP/XAML - Strange white "ribbon" shown in my UWP app?

Recently I'm trying to learn some Windows 10 UWP app developments. And now I encountered a strange issue, where there is a white ribbon in the bottom of my app, see here:
If I drag and enlarge the app window, then there will be another white ribbon on the top, see here:
Here is my XAML for the UI:
<Page
x:Class="ApodidaeCore.ClockMainUI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ApodidaeCore"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid RequestedTheme="Dark" Margin="0,0,0,38" Height="600" Background="Black">
<Grid.RowDefinitions>
<RowDefinition Height="439*"/>
<RowDefinition Height="161*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="205*"/>
<ColumnDefinition Width="819*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="hourTextBlock" HorizontalAlignment="Left" Height="191" TextWrapping="Wrap" Text="00" VerticalAlignment="Top" Width="205" Foreground="White" FontSize="170" Margin="15.2,98,0,0" Grid.Column="1" />
<TextBlock x:Name="clockSymbolTextBlock" HorizontalAlignment="Left" Height="141" TextWrapping="Wrap" Text=":" VerticalAlignment="Top" Width="38" Foreground="White" FontSize="105" FontWeight="Bold" Margin="244.2,130,0,0" Grid.Column="1"/>
<TextBlock x:Name="minuteTextBlock" HorizontalAlignment="Left" Height="197" TextWrapping="Wrap" Text="00" VerticalAlignment="Top" Width="218" Foreground="White" FontSize="170" Margin="308.2,98,0,0" Grid.Column="1"/>
<TextBlock x:Name="weatherInfoTextBlock" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="White" FontSize="25" Margin="188.2,331,0,0" Text="Unknown" Width="314" Height="33" Grid.Column="1"/>
<TextBlock x:Name="notificationTextBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="No new notification" VerticalAlignment="Top" Foreground="White" FontSize="25" Margin="188.2,369,0,0" Height="30" Width="314" Grid.Column="1"/>
<Image x:Name="weatherInfoIconImage" HorizontalAlignment="Left" Height="100" Margin="67.2,321,0,0" VerticalAlignment="Top" Width="100" Grid.Column="1"/>
</Grid>
What should I do to fix this? Any suggestions? Thanks in advance!
Jackson.
Got fixed, silly me!
I've wrongly set the grid height and the margin.
Remove those two variables will fix that issue.
Change this:
<Grid RequestedTheme="Dark" Margin="0,0,0,38" Height="600" Background="Black">
to this:
<Grid RequestedTheme="Dark" Background="Black">

Aligning ListBox ContentControl to the right - C# WPF

I have a ListBox with text and a corresponding Image. I would Like the Image to align right. How would i alter my XAML to achieve this? We are mainly looking at lbxBuiltInLevels. Thanks.
<Page x:Class="TheseusAndTheMinotaur.ChooseLevelPage"
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="TheseusAndTheMinotaur"
mc:Ignorable="d"
Title="ChooseLevelPage">
<Grid Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.Background>
<ImageBrush ImageSource="/TheseusAndTheMinotaur;component/Images/MainBackground.jpg"/>
</Grid.Background>
<ListBox x:Name="lbxLevels" HorizontalAlignment="Left" Height="100" Margin="10,78,0,0" VerticalAlignment="Top" Width="100"/>
<Button x:Name="btnExit" Content="Exit" HorizontalAlignment="Left" Margin="10,208,0,0" VerticalAlignment="Top" Width="75" Click="btnExit_Click"/>
<Button x:Name="btnLoad" Content="Load" HorizontalAlignment="Left" Margin="10,183,0,0" VerticalAlignment="Top" Width="75" Click="btnLoadCustomLevel_Click"/>
<Label x:Name="lblSavedLevels" Content="Custom Levels" HorizontalAlignment="Left" Margin="10,20,0,0" VerticalAlignment="Top" FontSize="34" FontFamily="Papyrus" FontWeight="ExtraBold"/>
<Label x:Name="lblBuiltInLevels" Content="Built-in Levels" Grid.Column="1" HorizontalAlignment="Left" Margin="44,20,0,0" VerticalAlignment="Top" FontSize="34" FontWeight="ExtraBold" FontFamily="Papyrus"/>
<ListBox x:Name="lbxBuiltInLevels" Grid.Column="1" Margin="10,78,0,0" VerticalAlignment="Top">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Grid x:Name="gridForLevels">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Margin="3" Text="{Binding Str}"/>
<ContentControl Grid.Column="1" Margin="0,0,10,0" Content="{Binding Image}" HorizontalAlignment="Right" HorizontalContentAlignment="Right" FlowDirection="RightToLeft"/>
</Grid>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Page>
Set this code in your listbox:
HorizontalContentAlignment="Stretch"
Should look like:
<ListBox x:Name="lbxBuiltInLevels" Grid.Column="1" HorizontalContentAlignment="Stretch" Margin="10,78,0,0" HorizontalContentAlignment="Stretch" VerticalAlignment="Top"

Categories

Resources