Problem with loading picture from website to WPF Image using URL - c#

I've made a snake game. As in the title - I've tried to load a picture from website to the source of WPF Image using URL. My goal was to load a random picture from website as a bitmap every time the snake collects the food on the map, but the most important thing was that it shouldn't be loaded when the program executes, but after the snake collects the food. So I've created a string array, where I placed 5 url links to the pictures from the website and then I wanted to use Random() to choose a random link to the picture and place it to the source of WPF Image. But when the snake collects the food, nothing happens. And my question is: is my code wrong? Here's my code:
XAML CODE
<Window x:Class="Snake_Game.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:Snake_Game"
mc:Ignorable="d"
Title="Snake The Game"
SizeToContent="WidthAndHeight"
ContentRendered="Window_ContentRendered"
KeyUp="Window_KeyUp">
<Border BorderBrush="Black" BorderThickness="8">
<Canvas Name="GameBoard"
ClipToBounds="True"
Background="Black"
Height="1000"
Width="1000">
<Rectangle x:Name="ScoreBoard"
Fill="#FF7B2D96"
Height="1000"
Canvas.Left="706"
Stroke="Black"
Width="294"/>
<Label x:Name="PointsPanel"
Content="POINTS"
Canvas.Left="804"
Canvas.Top="27"
Height="29"
Width="95"
FontWeight="Bold"
FontSize="14"
Background="#FFE72626"/>
<Image x:Name="ImagePanel"
Height="355"
Canvas.Left="724"
Canvas.Top="272"
Width="266"
Stretch="UniformToFill"
RenderOptions.BitmapScalingMode="Fant"
StretchDirection="DownOnly"
MaxHeight="355"
MaxWidth="266">
<Image.OpacityMask>
<ImageBrush/>
</Image.OpacityMask>
</Image>
<Label x:Name="Score"
Content=""
Canvas.Left="804"
Canvas.Top="61"
Height="50"
Width="95"
FontWeight="Bold"
FontSize="36"
FontFamily="SimSun"/>
</Canvas>
</Border>
</Window>
Method used to load a random picture (from string array):
private void GetRandomImage()
{
string[] images =
{
"http://cdn.pixabay.com/photo/2015/02/28/15/25/snake-653639_1280.jpg",
"http://cdn.pixabay.com/photo/2014/12/25/14/54/snake-579682_1280.jpg",
"http://cdn.pixabay.com/photo/2015/02/28/15/25/rattlesnake-653642_1280.jpg",
"http://cdn.pixabay.com/photo/2012/10/10/05/07/grass-snake-60546_1280.jpg",
"http://cdn.pixabay.com/photo/2010/12/14/16/46/snake-3237_1280.jpg"
};
BitmapImage bitmapImage = new BitmapImage(new Uri(images[rnd.Next(images.Length)]));
ImagePanel.Source = bitmapImage;
}

Just remove OpacityMask. You do not set ImageSource of ImageBrush, so there is no reason to have OpacityMask.
<Border BorderBrush="Black" BorderThickness="8">
<Canvas Name="GameBoard" ClipToBounds="True" Background="Black" Height="1000" Width="1000"
>
<Rectangle x:Name="ScoreBoard" Fill="#FF7B2D96" Height="1000" Canvas.Left="706" Stroke="Black" Width="294"/>
<Label x:Name="PointsPanel" Content=" POINTS" Canvas.Left="804" Canvas.Top="27" Height="29" Width="95" FontWeight="Bold" FontSize="14" Background="#FFE72626"/>
<Image x:Name="ImagePanel" Height="355" Canvas.Left="724" Canvas.Top="272" Width="266" Stretch="UniformToFill" RenderOptions.BitmapScalingMode="Fant" StretchDirection="DownOnly" MaxHeight="355"
MaxWidth="266">
</Image>
<Label x:Name="Score" Content="" Canvas.Left="804" Canvas.Top="61" Height="50" Width="95" FontWeight="Bold" FontSize="36" FontFamily="SimSun"/>
</Canvas>
</Border>

Related

Why is the window re-scaling/shrinking in output when no resize and size constraints are in place?

I'm just new and messing around with the WPF C#.
I noticed that the design view and have some minor differences. Most noticeable at the button.
Design View:
Ouput # 1:
Ouput # 2:
It seems that the window/container is resizing/shrinking even when both min and max width's and height's were set with fixed values and resize mode is set to NoResize.
I could easily deal with some of the controls by setting up the vertical and horizontal alignments since the adjustments are not too noticeable, but for the Add button the differences are noticeable from the supposed output.
I did try setting the min size of the button so that it would not be affected by the resizing and there would still be space for the margins but the button properly renders only a part of it. (please see output 1)
The only compromise that I could is to set both vertical and horizontal alignment to stretch and not put any size constraints on the button but the button would end up shrinking from its original size. (please see output 2)
Here's the XAML for the window.
<Window x:Class="Game_Viewer.NewApp"
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:Game_Viewer"
mc:Ignorable="d"
Title="Add New Program" Height="155" Width="250" ResizeMode="NoResize" Background="#FF515151" WindowStyle="ToolWindow" WindowStartupLocation="CenterScreen" MinHeight="155" MinWidth="250">
<Grid Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Image x:Name="appicon_img" Margin="10,10,0,0" Source="Resources/menu_bg.png" Stretch="Fill" Height="35" VerticalAlignment="Top" HorizontalAlignment="Left" Width="35" Grid.ColumnSpan="2"/>
<TextBox x:Name="apppath_txtbox" HorizontalAlignment="Left" Margin="10,55,0,0" Width="154" Foreground="White" Background="#FF6E6E6E" Text="Program Path" VerticalContentAlignment="Center" BorderBrush="#FF707070" MaxLines="1" Height="25" VerticalAlignment="Top" FontSize="10" Grid.ColumnSpan="2"/>
<Button Content="Search" Margin="169,55,10,0" Click="BrowseApp_Click" Foreground="White" Background="#FF515151" FontWeight="Medium" Height="25" VerticalAlignment="Top" FontSize="10" Grid.ColumnSpan="2"/>
<Button x:Name="add_btn" Content="Add Program" Margin="72,89,72,8" Foreground="White" Background="#FF515151" Click="AddProgram_Click" FontWeight="Medium" Grid.ColumnSpan="2" MinHeight="30" MinWidth="100"/>
<TextBox x:Name="appname_txtbox" Margin="50,10,10,0" TextWrapping="WrapWithOverflow" Foreground="White" Text="Program Name" MaxLines="1" IsUndoEnabled="True" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderBrush="#FF707070" FontWeight="Medium" Background="#FF6E6E6E" Height="35" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
</Grid>
</Window>
I would like to know a solution or round-about for this kind of issue.
It's not really a problem having this issue as it is only aesthetic but I would at least know the reason why this is happening. And could you refer me to good tutorials for wpf using c#.
Thanks in advance and have a wonderful day!

UI Elements disappearing from WPF at runtime

I am developing a WPF application that allows to connect two peers in a video chat. I have been designed the UI in the XAML designer for the last two months and everything went fine, when I ran the application it did show everything etc. Until yesterday, for absolutely no apparent reason.
Some part of the UI just does not show up anymore WHEN the code is running on another laptop. I insist on the fact that it did work before and now not anymore. I also did not change anything in the code that could have anything to do with that part of the UI, nor in the xaml.
I did check that I never mess up with any Visibility parameter in the code-behind as well, so I don't see any reason why the behavior of the application suddenly changed.
Does anyone have an idea why this would happen?
Here is the xaml:
<Window x:Class="RealSenseiConfFusion.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1000" Width="1500"
WindowStartupLocation="Manual"
Left="0" Top="0"
Closing="Window_Closing"
Loaded="Window_Loaded" >
<Grid Background="LightSteelBlue">
<Rectangle Margin="10,10,498.6,0" Name="rectangle1" Stroke="Black" RadiusX="9" RadiusY="9" Fill="LightSlateGray" Height="142" VerticalAlignment="Top" />
<Grid Name="callAndSyncGrid" Margin="26,24,1165,835" Background="LightGray">
<StackPanel>
<TextBlock HorizontalAlignment="Center">ABOUT CALL</TextBlock>
<TextBlock Name="myIpTextBlock">Your ip is:</TextBlock>
<Grid Name="callGrid">
<TextBox Height="23" HorizontalAlignment="Left" Name="txtIP" Background="GhostWhite" Foreground="Black" Width="221" />
<Button Height="23" HorizontalAlignment="Right" Name="btnCall" Width="75" Click="btnCall_Click">Call</Button>
</Grid>
<Button Name="btnSync" Content="Start Synchronization" HorizontalAlignment="Left" Width="120" Height="25" Click="btnSynchronize_Click"/>
<TextBlock Name="currentConvText"></TextBlock>
</StackPanel>
</Grid>
<Grid Name="visSetupGrid" Margin="650,24,515,835" Background="LightGray">
<StackPanel>
<TextBlock HorizontalAlignment="Center">VISUALIZATION SETUP</TextBlock>
<Grid>
<TextBlock HorizontalAlignment="Left"> Visualization block 1:</TextBlock>
<ComboBox Name="vis1Combo" HorizontalAlignment="Right">
<ComboBoxItem IsSelected="True">Vis1UC1</ComboBoxItem>
<ComboBoxItem>Vis1UC1_2</ComboBoxItem>
<ComboBoxItem>Vis1UC2</ComboBoxItem>
<ComboBoxItem>Vis1UC2_2</ComboBoxItem>
<ComboBoxItem>Vis2UC1</ComboBoxItem>
<ComboBoxItem>Vis2UC2</ComboBoxItem>
<ComboBoxItem>Vis2UC3</ComboBoxItem>
<ComboBoxItem>Vis3UC1</ComboBoxItem>
<ComboBoxItem>Vis3UC2</ComboBoxItem>
<ComboBoxItem>Vis3UC3</ComboBoxItem>
</ComboBox>
</Grid>
<Grid>
<TextBlock HorizontalAlignment="Left"> Visualization block 2:</TextBlock>
<ComboBox Name="vis2Combo" HorizontalAlignment="Right">
<ComboBoxItem>Vis1UC1</ComboBoxItem>
<ComboBoxItem>Vis1UC1_2</ComboBoxItem>
<ComboBoxItem>Vis1UC2</ComboBoxItem>
<ComboBoxItem>Vis1UC2_2</ComboBoxItem>
<ComboBoxItem>Vis2UC1</ComboBoxItem>
<ComboBoxItem IsSelected="True">Vis2UC2</ComboBoxItem>
<ComboBoxItem>Vis2UC3</ComboBoxItem>
<ComboBoxItem>Vis3UC1</ComboBoxItem>
<ComboBoxItem>Vis3UC2</ComboBoxItem>
<ComboBoxItem>Vis3UC3</ComboBoxItem>
</ComboBox>
</Grid>
<Grid>
<TextBlock HorizontalAlignment="Left"> Visualization block 3:</TextBlock>
<ComboBox Name="vis3Combo" HorizontalAlignment="Right">
<ComboBoxItem>Vis1UC1</ComboBoxItem>
<ComboBoxItem>Vis1UC1_2</ComboBoxItem>
<ComboBoxItem IsSelected="True">Vis1UC2</ComboBoxItem>
<ComboBoxItem>Vis1UC2_2</ComboBoxItem>
<ComboBoxItem>Vis2UC1</ComboBoxItem>
<ComboBoxItem>Vis2UC2</ComboBoxItem>
<ComboBoxItem>Vis2UC3</ComboBoxItem>
<ComboBoxItem>Vis3UC1</ComboBoxItem>
<ComboBoxItem>Vis3UC2</ComboBoxItem>
<ComboBoxItem>Vis3UC3</ComboBoxItem>
</ComboBox>
</Grid>
<Button Name="btnVisApply" HorizontalAlignment="Center" Click="btnVisApply_Click">Apply!</Button>
</StackPanel>
</Grid>
<Grid Name="otherPeerVideo" HorizontalAlignment="Left" VerticalAlignment="Top" Width="1000" Height="700" Margin="0,83,0,0">
<Grid Width="960" Height="540">
<Border BorderBrush="DarkOrange" BorderThickness="4">
<WindowsFormsHost Name="wfServer"/>
</Border>
</Grid>
</Grid>
<StackPanel Name="feedbackAndMyVideoStackPanel" Background="WhiteSmoke" Margin="1000,83,0,0" VerticalAlignment="Top" Width="454" Height="800">
<Grid Name="vis1Grid" Height="160"></Grid>
<Grid Name="vis2Grid" Height="160"></Grid>
<Grid Name="vis3Grid" Height="160"></Grid>
<Grid Name="myVideoGrid" VerticalAlignment="Bottom" Height="320">
<Grid HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="454" Height="300">
<Border BorderBrush="Blue" BorderThickness="4">
<WindowsFormsHost Name="myVideo" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="446" Height="292"/>
</Border>
</Grid>
<Button Name="myVideoHideButton" Click="myVideoHideButton_Click" HorizontalAlignment="Right" VerticalAlignment="Top" Width="80" Height="20">Hide/Unhide</Button>
</Grid>
</StackPanel>
</Grid>
Here is a screenshot of how it looks like in the designer (on both machines):
Here is a screenshot of how it looks like when the application is running on my machine:
And here a screenshot of how it looks like when the application is running on the laptop:
I compiled/tried your code.
When the window gets small, many components disappear.
Because of the margins.
Example of margins replacement
Before :
With margins for placement
<Grid Name="callAndSyncGrid" Margin="26,24,1165,835" Background="LightGray">
<!-- ... -->
</Grid>
<Grid Name="visSetupGrid" Margin="650,24,515,835" Background="LightGray">
<!-- ... -->
</Grid>
After
With margins for "breathing"
Placement is made with two columns (ColumnDefinitions and Grid.Column="1"; 0 by default).
Here each Column takes 50 % (50*) of the available room.
You can also express the size as "Auto" or 323 (hardcoded size)
<Grid Name="parentGridForLayout" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="50*"/>
</Grid.ColumnDefinitions>
<Grid Name="callAndSyncGrid" Margin="10" Background="LightGray">
<!-- ... -->
</Grid>
<Grid Name="visSetupGrid" Margin="10" Background="LightGray">
<!-- ... -->
</Grid>
</Grid>
Advice
Using Grid+margin or Canvas are pretty close approaches. But with Grid you can create Columns and Rows to organize layout. And you can compose Grids(or other Panels like StackPanels, DockPanels, WrapPanels) into Grids for organizing the layout.
Disclaimer :
My piece of code alone is not enough.
You need to organize all your GUI, with many panels.
I would recreate the window from scratch : empty page, then put a first Grid (or any suitable Panel), split it with columns or rows. Then put some content imported from previous window at the grid level then go in the children grid to put some more grids/imported content
Regards
As #HighCore and #Emmanuel DURIN pointed out, the problem was that I defined the layout with Margins, which can lead to big trouble if I run the application on different screen sizes.
To correct this, I used Canvas'es in order to place every element with respect to their containers.
Now I can use the application on different screens and everything gets displayed as I want it.
Here the better layouted UI for reference:
<Window x:Class="RealSenseiConfFusion.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="860" Width="1500"
WindowStartupLocation="Manual"
Left="0" Top="0"
Closing="Window_Closing"
Loaded="Window_Loaded" >
<Grid Background="LightSteelBlue">
<Canvas>
<Canvas Canvas.Top="10" Canvas.Left="10" Width="980" Height="142">
<Rectangle Width="980" Height="132" Name="rectangle1" Stroke="Black" RadiusX="9" RadiusY="9" Fill="LightSlateGray" VerticalAlignment="Top" />
<Grid Name="callAndSyncGrid" Canvas.Top="15" Canvas.Left="15" Width="200" Background="LightGray">
<StackPanel>
<TextBlock HorizontalAlignment="Center">ABOUT CALL</TextBlock>
<TextBlock Name="myIpTextBlock">Your ip is:</TextBlock>
<Grid Name="callGrid">
<TextBox Height="23" HorizontalAlignment="Left" Name="txtIP" Background="GhostWhite" Foreground="Black" Width="221" />
<Button Height="23" HorizontalAlignment="Right" Name="btnCall" Width="75" Click="btnCall_Click">Call</Button>
</Grid>
<Button Name="btnSync" Content="Start Synchronization" HorizontalAlignment="Left" Width="120" Height="25" Click="btnSynchronize_Click"/>
<TextBlock Name="currentConvText"></TextBlock>
</StackPanel>
</Grid>
<Grid Name="visSetupGrid" Canvas.Top="15" Canvas.Right="15" Width="200" Background="LightGray">
<StackPanel>
<TextBlock HorizontalAlignment="Center">VISUALIZATION SETUP</TextBlock>
<Grid>
<TextBlock HorizontalAlignment="Left"> Visualization block 1:</TextBlock>
<ComboBox Name="vis1Combo" HorizontalAlignment="Right">
<ComboBoxItem IsSelected="True">Vis1UC1</ComboBoxItem>
<ComboBoxItem>Vis1UC1_2</ComboBoxItem>
<ComboBoxItem>Vis1UC2</ComboBoxItem>
<ComboBoxItem>Vis1UC2_2</ComboBoxItem>
<ComboBoxItem>Vis2UC1</ComboBoxItem>
<ComboBoxItem>Vis2UC2</ComboBoxItem>
<ComboBoxItem>Vis2UC3</ComboBoxItem>
<ComboBoxItem>Vis3UC1</ComboBoxItem>
<ComboBoxItem>Vis3UC2</ComboBoxItem>
<ComboBoxItem>Vis3UC3</ComboBoxItem>
</ComboBox>
</Grid>
<Grid>
<TextBlock HorizontalAlignment="Left"> Visualization block 2:</TextBlock>
<ComboBox Name="vis2Combo" HorizontalAlignment="Right">
<ComboBoxItem>Vis1UC1</ComboBoxItem>
<ComboBoxItem>Vis1UC1_2</ComboBoxItem>
<ComboBoxItem>Vis1UC2</ComboBoxItem>
<ComboBoxItem>Vis1UC2_2</ComboBoxItem>
<ComboBoxItem>Vis2UC1</ComboBoxItem>
<ComboBoxItem IsSelected="True">Vis2UC2</ComboBoxItem>
<ComboBoxItem>Vis2UC3</ComboBoxItem>
<ComboBoxItem>Vis3UC1</ComboBoxItem>
<ComboBoxItem>Vis3UC2</ComboBoxItem>
<ComboBoxItem>Vis3UC3</ComboBoxItem>
</ComboBox>
</Grid>
<Grid>
<TextBlock HorizontalAlignment="Left"> Visualization block 3:</TextBlock>
<ComboBox Name="vis3Combo" HorizontalAlignment="Right">
<ComboBoxItem>Vis1UC1</ComboBoxItem>
<ComboBoxItem>Vis1UC1_2</ComboBoxItem>
<ComboBoxItem IsSelected="True">Vis1UC2</ComboBoxItem>
<ComboBoxItem>Vis1UC2_2</ComboBoxItem>
<ComboBoxItem>Vis2UC1</ComboBoxItem>
<ComboBoxItem>Vis2UC2</ComboBoxItem>
<ComboBoxItem>Vis2UC3</ComboBoxItem>
<ComboBoxItem>Vis3UC1</ComboBoxItem>
<ComboBoxItem>Vis3UC2</ComboBoxItem>
<ComboBoxItem>Vis3UC3</ComboBoxItem>
</ComboBox>
</Grid>
<Button Name="btnVisApply" HorizontalAlignment="Center" Click="btnVisApply_Click">Apply!</Button>
</StackPanel>
</Grid>
</Canvas>
<Grid Name="otherPeerVideo" HorizontalAlignment="Left" Canvas.Top="150" Canvas.Left="20">
<Grid Width="960" Height="540">
<Border BorderBrush="DarkOrange" BorderThickness="4">
<WindowsFormsHost Name="wfServer"/>
</Border>
</Grid>
</Grid>
<StackPanel Name="feedbackAndMyVideoStackPanel" Background="WhiteSmoke" Canvas.Right="15" Canvas.Top="15" VerticalAlignment="Top" Width="454" Height="800">
<Grid Name="vis1Grid" Height="160"></Grid>
<Grid Name="vis2Grid" Height="160"></Grid>
<Grid Name="vis3Grid" Height="160"></Grid>
<Grid Name="myVideoGrid" VerticalAlignment="Bottom" Height="320">
<Grid HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="454" Height="300">
<Border BorderBrush="Blue" BorderThickness="4">
<WindowsFormsHost Name="myVideo" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="446" Height="292"/>
</Border>
</Grid>
<Button Name="myVideoHideButton" Click="myVideoHideButton_Click" HorizontalAlignment="Right" VerticalAlignment="Top" Width="80" Height="20">Hide/Unhide</Button>
</Grid>
</StackPanel>
</Canvas>
</Grid>

Adding a background image in Windows Store Apps

Recently I started making a Windows Store App in XAML and in C#. Unfortunately I failed with adding a background png image stored in '/Assets'. Looked everywhere for any glues but I couldn't find anything helpful.
Here is my code:
<Page
x:Class="BoggleV01.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BoggleV01"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Margin="160,100" Width="1040">
<Grid.Background>
<ImageBrush ImageSource="/Assets/Background.png" Stretch="UniformToFill" />
</Grid.Background>
<StackPanel x:Name="levelsStackPanel" HorizontalAlignment="Left" Height="568" VerticalAlignment="Top" Width="260"/>
<StackPanel x:Name="playersStackPanel" HorizontalAlignment="Left" Height="568" VerticalAlignment="Top" Width="260" Margin="260,0,0,0"/>
<StackPanel x:Name="statisticsStackPanel" HorizontalAlignment="Left" Height="568" VerticalAlignment="Top" Width="260" Margin="520,0,0,0"/>
<StackPanel x:Name="settingsStackPanel" HorizontalAlignment="Left" Height="568" VerticalAlignment="Top" Width="260" Margin="780,0,0,0"/>
</Grid>
</Page>
you can use the below mentioned code
<ImageBrush ImageSource="/Applicationname;component/Assets/Background.png"
Stretch="UniformToFill" />

WPF Window - Fade Different parts of the same window

I have a basic WPF windows with the markup as specific below:
<Window x:Class="Application.SomeWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SomeWindow"
Topmost="True" WindowStyle="None" Height="39" Width="400"
ResizeMode="NoResize" ShowInTaskbar="False"
WindowStartupLocation="Manual" Background="Transparent"
Closing="Window_Closing" AllowsTransparency="True" Opacity="0">
<Border Background="CornflowerBlue" BorderBrush="Black" BorderThickness="0,0,0,0" CornerRadius="5,5,5,5" Opacity="0.75">
<Grid>
<!-- Display bar -->
<Image Grid.Row="1" Height="24" Margin="7,7,0,0" Name="img1" Stretch="Fill" VerticalAlignment="Top" Source="/Application;component/Images/dashboard/1.png" HorizontalAlignment="Left" Width="13" />
<Image Height="24" Margin="19,7,47,0" Name="image21" Source="/Application;component/Images/dashboard/2.png" Stretch="Fill" VerticalAlignment="Top" Grid.Row="1" Grid.ColumnSpan="2" />
<!-- Button 1 -->
<Button Style="{DynamicResource NoChromeButton}" Height="27" Margin="0,5,25,0" Name="btn1" Click="btn1_Click" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" Width="23" ToolTip="1">
<Image Height="26" HorizontalAlignment="Right" Name="img1" Source="/Application;component/Images/dashboard/3.png" VerticalAlignment="Top" Width="22" Stretch="Fill" />
</Button>
<!-- Button 2 -->
<Button Style="{DynamicResource NoChromeButton}" Height="27" Margin="0,5,5,0" Name="btn2" Click="btn2_Click" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" Width="23" ToolTip="2">
<Image Height="26" HorizontalAlignment="Right" Name="img2" Source="/Application;component/Images/dashboard/4.png" VerticalAlignment="Top" Width="22" Stretch="Fill" />
</Button>
</Grid>
</Border>
</Window>
Here is what it looks like now:
What I'd really like to do is make it so that initially looks like this:
Then, once mouseover happens, to fade background opacity in from 0 so it looks like the first image. The problem is that if I set the Border or Grid Background color to Transparent with the goal of fading in on mouseover, then everything inside the Border or Grid is affected as well.
Is there a way to manage the opacities of window and its UI elements seperately? Or perhaps there is a totally different route to take to get this background fade on mouseover? Thanks.
There are two options. Number one is to just move the outer border inside the grid, as the first child (and have the other controls alongside it, not in it). That way it will fade by itself, but still be behind the other controls. You will of course either have to set ColumnSpan/RowSpan, or wrap the entire thing in another Grid.
The second option is to just fade the background, not the entire border:
<Border ...>
<Border.Background>
<SolidColorBrush Color="CornflowerBlue" Opacity="0.5"/>
</Border.Background>
...
try this trick - draw a rectangle or border with dimensions bind to parent or ElementName.
It won't affect rest of elements of tree. Works for me.
<Grid x:Name="abc">
<Border
Width="{Binding ActualWidth, ElementName=abc}"
Height="{Binding ActualHeight, ElementName=abc}"
Background="Blue"
Opacity="0.5"/>
//buttons or inner grid
...
</Grid>
If you don'w want to use ElementName, simply replace Width and Height by
Width="{Binding ActualWidth, Source={RelativeSource Mode=FindAncestor, AncestorType=Grid}}"
Height="{Binding ActualHeight, Source={RelativeSource Mode=FindAncestor, AncestorType=Grid}}"

Inconsistent behavior of Textblock in Silverlight

Hi all,
I found an issue with textblock in my Silverlight application. As observed from the picture I attached, in the "How to play" silverlight page, the title are bound to disappear if I navigate from different route.
Please note that that is static textblock with static wording. Just that from different navigation point, the header wording is missing!
I navigate from screen to screen using this.content = new something; (e.g. : this.content = new instruction())
Can someone guide me on how to overcome this issue? All my page is not usercontrol, but just silverlight page.
<navigation:Page x:Class="SpotTheDifference.UI.Game.Silverlight.instruction"
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"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="893" d:DesignHeight="670"
Title="BonusLink Spot 'd' Difference">
<Grid x:Name="LayoutRoot">
<Image x:Name="imgBG" HorizontalAlignment="Left" Height="670" VerticalAlignment="Top" Width="893" Source="/img/bg/dialogbox-bg.jpg" Visibility="Visible"/>
<Image x:Name="imgPlayGame" HorizontalAlignment="Left" Height="50" Margin="731,209,0,0" VerticalAlignment="Top" Width="145" Source="/img/btn/btn_play_red.png"/>
<Button x:Name="btnPlay" Content="" HorizontalAlignment="Left" Margin="731,209,0,0" VerticalAlignment="Top" Width="145" Height="50" Opacity="0" Click="btnPlay_Click" MouseEnter="btnPlay_MouseEnter" MouseLeave="btnPlay_MouseLeave"/>
<Image x:Name="imgScoreBoard" HorizontalAlignment="Left" Height="50" Margin="731,264,0,0" VerticalAlignment="Top" Width="145" Source="/img/btn/btn_scoreboard_red.png"/>
<Button x:Name="btnScoreBoard" Content="" HorizontalAlignment="Left" Margin="731,264,0,0" VerticalAlignment="Top" Width="145" Height="50" Opacity="0" Click="btnScoreBoard_Click" MouseEnter="btnScoreBoard_MouseEnter" MouseLeave="btnScoreBoard_MouseLeave"/>
<Image x:Name="imgProfile" HorizontalAlignment="Left" Height="50" Margin="731,319,0,0" VerticalAlignment="Top" Width="145" Source="/img/btn/btn_profile_red.png"/>
<Button x:Name="btnProfile" Content="" HorizontalAlignment="Left" Margin="731,319,0,0" VerticalAlignment="Top" Width="145" Height="50" Opacity="0" Click="btnProfile_Click" MouseEnter="btnProfile_MouseEnter" MouseLeave="btnProfile_MouseLeave"/>
<Image x:Name="imgHowToPlay" HorizontalAlignment="Left" Height="50" Margin="731,374,0,0" VerticalAlignment="Top" Width="145" Source="/img/btn/btn_howto_green.png"/>
<Image x:Name="imgLogOut" HorizontalAlignment="Left" Height="50" Margin="731,429,0,0" VerticalAlignment="Top" Width="145" Source="/img/btn/btn_logout_red.png"/>
<Button x:Name="btnLogOut" Content="" HorizontalAlignment="Left" Margin="731,429,0,0" VerticalAlignment="Top" Width="145" Height="50" Opacity="0" Click="btnLogOut_Click" MouseEnter="btnLogOut_MouseEnter" MouseLeave="btnLogOut_MouseLeave"/>
<TextBlock x:Name="lblDesc" HorizontalAlignment="Left" Margin="145,250,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="505" Height="195" Foreground="#FF007EFF" FontFamily="Comic Sans MS" FontSize="14.667" FontWeight="Bold">
<Run Text="There will be a set of 2 almost identical pictures revealed every month for players to identify 10 differences each."/>
<LineBreak/>
<Run/>
<LineBreak/>
<Run Text="With every correct click, a circle will appear on the difference spot; whereas every wrong click, the timer will be further reduced."/>
<LineBreak/>
<Run/>
<LineBreak/>
<Run Text="Total play time for this game is 30 seconds."/>
<LineBreak/>
<Run Text="(Hint: The faster you identify the differences, the higher the score)"/>
</TextBlock>
<TextBlock x:Name="lblHeader" Text="Spot 'd' Difference: How to Play Instruction" Margin="146,214,275,427" FontFamily="Comic Sans MS" FontSize="21.333" FontWeight="Bold" Width="472" Height="29" />
</Grid>

Categories