Button to change background colour - xaml

I'm developing a UWP app. I would like to have a radio button change the background of the mainpage and settings page when clicked. The radio button code below is in Settings.xaml.cs
private void BGRadioButtonGreen_Checked(object sender, RoutedEventArgs e)
{
MainPage.Background = Brushes.Red??;
}
In this example I'm trying to select a radio button and have it change the settings and mainpage background to Green.However it doesn't seem to be working. Thanks in advance

You can do it be setting background color of root grid. Like in example below, after radio button checked color would change to red. This should be what you're looking for.
<Page
x:Class="Test.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Test"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Background="Blue">
<Grid x:Name="MainGrid" Background="Green">
<StackPanel>
<RadioButton Content="Red" Tag="Green" Checked="RadioButton_Checked"/>
</StackPanel>
</Grid>
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
MainGrid.Background = new SolidColorBrush(Windows.UI.Colors.Red);
}

For changing background color, as #ColdMorningLight mentioned, you should set a SolidColorBrush value to the Background property.
Since you attempt to change the MainPage background color from Settings page, which is in different pages, you also need to get the current MainPage instance firstly and then set the page's background. For example:
MainPage mainPage;
public SettingPage()
{
this.InitializeComponent();
mainPage = (Window.Current.Content as Frame).Content as MainPage;
}
private void radchangecolor_Checked(object sender, RoutedEventArgs e)
{
mainPage.Background = new SolidColorBrush(Windows.UI.Colors.Green);
}
Pay attention that I set the page's background color, you only can see the effects when the root panel background color is transparent (the default value is Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" ). Otherwise you need to change the root panel background color. For example, MainPage as follows will take effects:
<Page
...
mc:Ignorable="d" >
<StackPanel>
<Frame x:Name="frame" Height="200" Width="300" Background="Azure"></Frame>
<Button x:Name="btntest" Click="btntest_Click" Content="test"></Button>
</StackPanel>
</Page>

Related

How to handle NavigationView PaneFooter Settings item click

When adding a NavigationView a 'Settings' item is automatically added at the bottom of the NavigationView. How can a click event be added to this item since it doesn't appear to be accessible from XAML?
<Page
x:Class="My_Project.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<NavigationView>
<NavigationView.PaneFooter>
</NavigationView.PaneFooter>
</NavigationView>
</Grid>
</Page>
How can a click event be added to this item since it doesn't appear to be accessible from XAML?
The Settings button click event is already handled by the NavigationView.ItemInvoked Event. This is mentioned in the document NavigationView as "When the user taps on a navigation item, the navigation view shows that item as selected and raises an ItemInvoked event. "
So you just need to handle the NavigationView.ItemInvoked Event and check the NavigationViewItemInvokedEventArgs.IsSettingsInvoked Property.
Here is the code that you could refer to:
<Grid>
<NavigationView ItemInvoked="NavigationView_ItemInvoked">
<NavigationView.PaneFooter>
</NavigationView.PaneFooter>
</NavigationView>
</Grid>
In code behind:
private void NavigationView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
{
if (args.IsSettingsInvoked == true)
{
//do something
}
}
Update
If you want to check if the item is a specific one, you could set a tag property in the Xaml for the navigationview item and then check this property in the ItemInvoked event.
In xaml:
<NavigationViewItem Tag="home" Icon="Home" Content="Home"/>
In code behind:
private void NavigationView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
{
var navItemTag = args.InvokedItemContainer.Tag.ToString();
if (navItemTag.Equals("home"))
{
// do something
Debug.WriteLine("test");
}
}

UWP: How to fix focus on particular UI element so that i can receive key press events?

So i have chat window with a text box, a mic button and some other buttons for settings and sharing. So i want to always switch on the mic whenever space button is pressed, except when the textbox has focus. No matter if other buttons are clicked last, space-bar should turn-on the mic.
Any help or pointers?
Here is a working sample.
Code Behind
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
if (args.VirtualKey == Windows.System.VirtualKey.Space && txtData.FocusState == FocusState.Unfocused))
{
txtData.Text = "Mike Called";
}
}
}
txtData here is my TextBox that I an checking if I have FocusState Unfocused
XAML
<Page
x:Class="App12.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App12"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBox Text="Hello World" Name="txtData" Width="300" Height="35" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Page>
You should subscribe the KeyDown event of Window.Current.CoreWindow. It will be fired everytime user press any key, regardless of focused control. Only your app has to be focused.
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
private void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
{
if (args.Handled)
{
return;
}
// Your event handling
}

Create a component and reuse it in Windows phone 8.1

I want to create some components and reuse them in different pages, put more than one in a page, etc.
For example, I want to create a component that contains an image, some text, etc. The position of the elements are fixed, but I will change the image, the text... I mean, in a same page I want to put three circles with different image and text...
What is the best way to do it? I've found UserControl, but I'm unable to call a method from another page to change something.
This is my component XAML
<UserControl
x:Class="aa.Components.CircularGraph"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Components"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300">
<Grid Name="view">
<Image Name="imageGraph" Source="../Assets/aa/circuloGris.png"
/>
<StackPanel Orientation="Vertical">
<TextBlock Name="firstLine" Text="1" FontFamily="Arial" FontSize="9"></TextBlock>
<TextBlock Name="secondLine" Text="2" FontFamily="Arial" FontSize="9"></TextBlock>
<TextBlock Name="thirdLine" Text="3" FontFamily="Arial" FontSize="9"></TextBlock>
</StackPanel>
</Grid>
</UserControl>
Its code:
public sealed partial class CircularGraph: UserControl {
public CircularGraph() {
this.InitializeComponent();
Height = 300;
Width = 400;
}
public void changeFirstLine(string var) {
firstLine.Text = var;
}
}
In other page I put:
<local:CircularGraph Name="circularGraph"/>
And I've tried to put this in .cs:
protected override void OnNavigatedTo(NavigationEventArgs e) {
circularGraph.changeFirstLine("aaa");
}
But I have an error: The name 'circularGraph' does not exits in the current context.
How can I do this?
Sorry if it's a simple question. I'm newbie at Windows phone.
Thank you very much!
Try x:Name instead of Name. "All x:Name means to XAML is generate a field to store the value in the code behind class."
<local:CircularGraph x:Name="circularGraph"/>
In WPF, what are the differences between the x:Name and Name attributes?

Is it possible to show default image before original load from server?

I want to show default image (some thing like brand icon) before original image is loaded from server. It is possible with out writing much code. Because I want same behavior in whole app.
or we need to create custom control for that. Please guide me!
If you are going to reuse it at a lot of different place it's probably easier to just create a CustomControl.
Here is a small user control which should do that:
<UserControl x:Class="PhoneApp1.ImageWithLoading"
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"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480"
x:Name="myImageWithLoading">
<Grid x:Name="LayoutRoot" >
<Image x:Name="temporaryImage" Source="/Assets/Loading"/>
<Image Source="{Binding Source,ElementName=myImageWithLoading}" ImageOpened="RemoteImage_OnLoaded"/>
</Grid>
</UserControl>
public partial class ImageWithLoading : UserControl
{
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Source", typeof (ImageSource), typeof (ImageWithLoading), new PropertyMetadata(default(ImageSource)));
public ImageSource Source
{
get { return (ImageSource) GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
public ImageWithLoading()
{
InitializeComponent();
}
private void RemoteImage_OnLoaded(object sender, RoutedEventArgs e)
{
temporaryImage.Visibility = Visibility.Collapsed;
}
}
Another option might be to create a default style for Images in your default style page like so
<Style TargetType="Image">
<Setter Property="Source" Value="/Assets/Load.jpg"/>
</Style>
and just set the source when the image is ready

Windows Phone 7 XAML -- getting a binding to work with the container of my object

What I want to do is bind the text of a TextBlock to my custom ButtonSymbol property of the UserControl.
Here is the XAML for the UserControl. The Binding part for the TextBlock needs to be filled in.
<UserControl
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"
x:Class="Calculator.CalculatorButton"
d:DesignWidth="120" d:DesignHeight="80">
<Grid x:Name="LayoutRoot" Background="Transparent">
<Image Source="buttonb#2x.png" Stretch="Fill"/>
<Button x:Name="InvisibleButton" Content="{Binding ButtonSymbol}" Margin="0,0,0,0" d:LayoutOverrides="Width, Height" BorderThickness="1" Click="InvisibleButton_Click"/>
<TextBlock HorizontalAlignment="Center" Margin="0,0,0,0" TextWrapping="Wrap"
Text="{Binding ????????}"
VerticalAlignment="Top"/>
</Grid>
</UserControl>
And here is the CodeBehind:
namespace Calculator
{
public partial class CalculatorButton : UserControl
{
public string ButtonSymbol {get; set;}
public CalculatorButton()
{
// Required to initialize variables
InitializeComponent();
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
// TODO: Add event handler implementation here.
}
private void InvisibleButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
Debug.WriteLine(#"click");
Debug.WriteLine(ButtonSymbol);
// TODO: Add event handler implementation here.
}
}
}
Note that this is WP7 with Silverlight, and the RelativeSource class is not the same as in other versions.
You need to set the DataContext of the user control.
If you add this:
this.DataContext = this;
into the constructor or Loaded event of the user control you can then do this:
Text="{Binding ButtonSymbol}"
Note that you can also declaratively bind the DataSource of the XAML, this is just an easy programmatic way to do it.