Error when trying to set Grid.Background to accent colour - c#

In my application the user can choose what colour they would like a particular feature of the app to be.
They can choose from a variety of colours which works fine, however when trying to set the Grid's background to the accent colours; when chosen, a NullReferenceException was unhandled error appears.
The code I am using is:
Color accentColour = (Color)Application.Current.Resources["PhoneAccentColor"];
gridColour.Background = new SolidColorBrush(accentColour);
Anyone know what I am doing wrong? (I've also tried using a Rectangle and it's .Fill property).
Thanks.
[SOLVED: Post solved in comments.]

You can try that code in OnNavigatedTo Event. Its Working.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
Color accentColour = (Color)Application.Current.Resources["PhoneAccentColor"];
ContentPanel.Background = new SolidColorBrush(accentColour);
}

I guess you can easily do this by binding the PhoneAccent color within your XAML for your Grid.
These threads would be helpful:
Windows Phone 8 Change Accent and Theme Colour
windows phone 8 xaml set the color of a button on click
Hope it helps!

Related

How to Override Application Bar Theme using Jeff Wilcox's PhoneThemeManager

I have recently added Jeff Wilcox's PhoneThemeManager to my app, which works great, although I am having problems changing the application bar buttons to the color I wish to use. My app design specifications require using the Light Theme. The problem I am having is that when the device is already in light theme, upon application loading the appbar foreground is the correct color because the PhoneThemeManager does not overwrite any values, although when in dark theme the appbar foreground values are black (which mimics the default foreground settings in the light theme).
According to his website, "If you have code in your app like “var ab = new ApplicationBar”, beware that that application bar will take on the system’s actual theme colors by default, and not the overridden light/dark coloring that happens with the app.
If you need to new up an ApplicationBar, you should use the convenience method of ThemeManager.CreateApplicationBar() or use the extension method on app bar that I added, MatchOverriddenTheme, to set the color values."
I'm not sure how to implement this to get custom appbar button colors.
I've done the following
public App()
{
// Global handler for uncaught exceptions.
UnhandledException += Application_UnhandledException;
// Standard Silverlight initialization
InitializeComponent();
// Phone-specific initialization
InitializePhoneApplication();
ThemeManager.ToLightTheme();
// Other code that might be here already...
}
And in my MainPage appbar I wish to change the buttons accordingly
private void BuildLocalizedApplicationBar()
{
// Set the page's ApplicationBar to a new instance of ApplicationBar.
ApplicationBar = new ApplicationBar();
ApplicationBar.ForegroundColor = Color.FromArgb(255, 35, 85, 155);
//Not sure where or how to use this?
ApplicationBar.MatchOverriddenTheme(); //to override ThemeManager defaults
//Create appbar buttons
...
}
Any ideas?

Datagrid cell tooltip and foreground setting in CellEditEnding - it's moving, why?

I'm using the CellEditEnding event for setting new foreground color and tooltip for editted cells. It works quite ok, except why I scroll down in the datagrid the foreground colour and tooltip moves in the column and that makes it quite useless.
I think it must be something with the FrameWork element but I'm quite not sure how to solve this problem.
My code:
private void myDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
System.Windows.Controls.ToolTip tt = new System.Windows.Controls.ToolTip();
tt.Content = "My tooltip text";
FrameworkElement element = (e.Column.GetCellContent(e.Row));
System.Windows.Controls.DataGridCell chosen = (element.Parent as System.Windows.Controls.DataGridCell);
chosen.ToolTip = tt;
chosen.Foreground = new SolidColorBrush(Colors.Red);
}
Can anyone tell me why it's moving(the tooltip and color) to other cells when scrolling in the datagrid? And how shoud I solve it?
Or if you have better idea on how to set the tooltip and foreground color for editted cells in datagrid, please tell me.
Thanks for any help in advance ;)
Try using:
<DataGrid Name="SimpleDataGrid" ScrollViewer.CanContentScroll="False" CellEditEnding="SimpleDataGrid_CellEditEnding" />
for scrolls in terms of physical units.
For more information see MSDN.

Why am I getting "Control does not support transparent background colors"?

I am working on a C# - Winforms application and attempting to set the background colour of a read-only text box like so...
txtMyBox.BackColor = Color.FromName ("Red");
This is failing with the error message...
System.ArgumentException was unhandled
Message=Control does not support transparent background colors.
Source=System.Windows.Forms
First things first; is this the right way to set the background colour for a read-only textbox? I'm doing this a lot for ordinary textboxes and it appears to work fine.
If it is, could someone help me out with what a "transparent background color" is, and why I should want one? I don't think I do; I just want the background to change colour.
Quite old post but... Have you tried this before?
public partial class MyForm : Form
{
public MyForm()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
InitializeComponent();
}
}
A bit late - but eventually helps this someone who - like me - found this page according to the OPs question:
I got this error when Setting a Winforms Splitter background color which was generated by a
Color.FromArgb(0xC9,0xD9,0xEB);
The solution was to generate the Color value instead with the following helper method:
ColorTranslator.FromHtml("#C9D9EB")
This avoids generating a transparency Information.
A better way would be:
txtMyBox.BackColor = Color.Red;
The error you getting is being caused because of somewhere else in your code where you are trying to set the background color of the form itself to be transparent and that is not supported, check your code carefully and you will find somthing like that:
BackColor = Color.Transparent;
Since there is no element name (i.e. myControl.BackColor = somthing) and your sentence start with BackColor = somthing - the compiler thinks you want to change the background color of the windows form into being transparent, check your form editor too - you might doing that from there.
Here is a reproduction of your error:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BackColor = Color.Transparent; //Control does not support transparent background colors.
}
}
Try this instead of FromName
txtMyBox.BackColor = Color.Red;
ColorTextBox.BackColor = colorDialog1.Color;
textBox2.BackColor = System.Drawing.Color.FromArgb(
ColorTextBox.BackColor.ToArgb());

Refresh a control

I am trying to change a link label's fore color but the color won't graphically change.
I have a timer that updates the fore color of the control
private void Timer_Tick(object sender, EventArgs e)
{
MyLbl.ForeColor = shouldUpdate? Color.Blue: Color.Gray;
}
The update is successful and while debugging, I can see that the fore color property of myLbl is different. So why doesn't it change it graphically?
I also tried
MyLbl.ForeColor = Color.Gray;
And tried adding Application.DoEvents() after the change of the fore color.
Any solutions?
Unlike vanilla labels, link-labels don't use the ForeColor property in this manner to colour their text.
Use the LinkColor property instead.
Gets or sets the color used when displaying a normal link.
In your case, you need:
MyLbl.LinkColor = shouldUpdate? Color.Blue: Color.Gray;
Note that this not an update problem - you don't have to explicitly call Application.DoEvents (which is almost never the right thing to do) or Invalidate or Refresh to get the link-label to respond to the colour-change.

How to hide textboxes, labels and buttons C# WPF

I would like to hide several textboxes, a label and a button as soon as a button is clicked... however, for some reason, my code doesn't seem to cause this effect. Nothing appears to happen. I'm using WPF.
Here is my code:
private void doSomething_Click(object sender, RoutedEventArgs e)
{
Name.Visibility = Visibility.Hidden;
}
This code doesn't seem to work.. any ideas?
I believe Visibility.Collapsed is what you need and not Visibility.Hidden.
EDIT: Did you try follow up this code with UpdateLayout() method of parent element/component?
Your code seems to work fine, the "Signing in..." label appears after everything else disappear. I suggest you to just copy all your code from the .xaml.cs file and the .xaml file into a new project, but make sure you don't copy the first line"<Window x:Class="..." because it could generate an error if the class name isn't the same in the new project.
For the xaml code I suggest you not think the same as you design windows forms applications. WPF has the layout system, which re-orientates or re-sizes its elements when re-sizing the window. So you should not specify exact numbers in the margin property as if they where coordinates. Create a grid, create rows or columns for each element and then just set the horizontal or vertical alignment or margins. Think different than the old windows forms way.
I've run your code... and it's working great for me. I've not changed anything (except the variable names) so I guess it's a bug from VS.
As said nikolamm94 try to add this.UpdateLayout(); at the end of connect_Click it might help. I tried and it is still working fine. Or maybe create a new VS projet, it already worked for me a few times.
Sorry my answer is not the most helpful, I wanted to put a comment instead but I don't have enough reputation :/
Please refer: https://msdn.microsoft.com/en-us/library/ms748821(v=vs.85).aspx
Set to Visible: tb1.Visibility = System.Windows.Visibility.Visible;
Set to Hide: tb1.Visibility = System.Windows.Visibility.Hidden;
You can hide a textbox by going to properties->appearance->visibility, then setting it to "hidden"

Categories