AppBar Button Changing icon dynamically - c#

I have a button on appBar. When user clicks it, it should change the icon. It will either show the Map or List so it needs to toggle between those 2.
If I use the code below with icons Symbol.Play & Symbol.Stop it toggles perfectly.
But when I use Symbol.Map & Symbol.List it does not toggle properly. (On simulator at least.) It changes sometimes, but mostly stays the same icon.
Code:
private void MapToggle_Click(object sender, RoutedEventArgs e)
{
if (JobMap.Visibility != Visibility.Visible)
{
MapToggle.Icon = new SymbolIcon(Symbol.List);
MapToggle.Label = "List";
JobMap.Visibility = Visibility.Visible;
}
else
{
MapToggle.Icon = new SymbolIcon(Symbol.Map);
MapToggle.Label = "Map";
JobMap.Visibility = Visibility.Collapsed;
}
}
Binded to XAML
AppBarButton Name="MapToggle" Icon="Map" Click="MapToggle_Click" Label="Map"

Related

How to change Fontawesome.wpf Icon in c# code

I'm fairly new to programming in wpf, so this might be pretty basic, but I have no Idea how to change a FontAwesome Icon of a button dynamically (in c# code). I use the nuget package as described in this post here. However the last part:
And finally in your C# code behind:
using FontAwesome.WPF; // on the top of the code
btnButton.Content = FontAwesomeIcon.LongArrowRight;
is not working for me, as it only shows the text "LongArrowRight" after clicking the button.
This is my code:
<Window
...
xmlns:fa="http://schemas.fontawesome.io/icons/">
<Grid>
<Button x:Name="btnPlay" Click="btnPlay_Click">
<Button.Content>
<fa:ImageAwesome Icon="Play"/>
</Button.Content>
</Button>
</Grid>
</Window>
using FontAwesome.WPF; // at the top
private bool running = false;
private void btnPlay_Click(object sender, RoutedEventArgs e)
{
if (running)
{
running = false;
btnPlay.Content = FontAwesomeIcon.Play;
}
else
{
running = true;
btnPlay.Content = FontAwesomeIcon.Stop;
}
}
As already stated when the button is clicked it does not show the Icon but only the text "Stop" and when pressed again "Play".
FontAwesomeIcon is enum. When you assign enum value to Button.Content it will be displayed as text.
You need to assign ImageAwesome element to Content:
btnPlay.Content = new ImageAwesome { Icon = FontAwesomeIcon.LongArrowRight };
btnPlay.Content = new ImageAwesome { Icon = FontAwesomeIcon.Play };
btnPlay.Content = new ImageAwesome { Icon = FontAwesomeIcon.Stop };

Changing a color of a icon from an action on an other page

I am a beginner on C#, I am coding an UWP app on Visual Studio, mostly for Windows 10 platforms.
Here's my problem:
I have a navigation view on the main page, which lists a few subjects.
Clicking on one of them brings you to a page (Page1) where I have radio buttons, that changes the color of the title of this Page1.
I would like to have this color applied to the subject on the main page
Can't figure out how to do...
Any tips?
Thanks!
You can access the NavigationView in MainPage.xaml.cs by creating a static instance of MainPage and change the color.
Here is the solution:
MainPage.xaml
<NavigationView x:Name="nvSample"
x:FieldModifier="Public">
...
</NavigationView>
MainPage.xaml.cs
public static MainPage Current;
public MainPage()
{
this.InitializeComponent();
Current = this;
}
Page1.xaml.cs
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
var nav = MainPage.Current.nvSample;
nav.Foreground = new SolidColorBrush(Colors.Yellow);
}
Best regards.
Thank you very much Richard, I made it :)
The Page1.xaml.cs code changed all the texts foreground in my page though, so I used this:
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
RadioButton radio = (RadioButton)sender;
{}
if (radio != null)
{
String selected = radio.Tag.ToString();
switch (selected)
{
default:
case "Red":
Title.Foreground = new SolidColorBrush(Colors.Red);
MainPage.Current.Subject1.Foreground = new SolidColorBrush(Colors.Red);
break;`
Thank you again.
Now next mission for me, saving those colors after the application is closed so they stay the same when the app is lunched. If you have any advices... :)

How to refresh ToolTipText on ToolStripButton?

I want to implement simple toggle functionality on a ToolStripButton.
View Mode --> Edit Mode
Edit Mode --> View Mode
This piece of code is working fine except that ToolTip doesn't refresh. I tried AutoToolTip property. I also tried setting ToolTipText to string.Empty. But my toolstrip continues to show old tooltip text. ToolTipText should also change when I toggle.
private void btnTrackingMode_Click(object sender, EventArgs e)
{
if (_currentSheetTrackingMode == SheetTrackingMode.ViewMode)
{
_currentTrackingMode = TrackingMode.EditMode;
btnTrackingMode.AutoToolTip = true;
btnTrackingMode.ToolTipText = string.Empty;
btnTrackingMode.ToolTipText = "You are currently in Edit Mode. Click here to enter into View Mode";
btnTrackingMode.Image = ((System.Drawing.Image) ((Image) new ComponentResourceManager(typeof(MyForm)).GetObject("btnTrackingEditMode.Image")));
btnTrackingMode.ImageTransparentColor = System.Drawing.Color.Magenta;
}
else
{
_currentTrackingMode = TrackingMode.ViewMode;
btnTrackingMode.AutoToolTip = true;
btnTrackingMode.ToolTipText = string.Empty;
btnTrackingMode.ToolTipText = "You are currently in View Mode. Click here to enter into Edit Mode";
btnTrackingMode.Image = ((System.Drawing.Image) ((Image) new ComponentResourceManager(typeof(MyForm)).GetObject("btnTrackingViewMode.Image")));
btnTrackingMode.ImageTransparentColor = System.Drawing.Color.Magenta;
}
}
You can try to refresh the button's parent ToolStrip
btnTrackingMode.ToolTipText = "something";
//put your toolstrip's name here
buttonsToolStrip.Refresh();
You should be able to access the parent tool strip through btnTrackingMode.Owner if it's not directly available for some reason.

Click event for the "..." three dots of the application bar to change the opacity

I just can not find how to generate a click event for the 3-dots-button of the application bar. the start mode of it is minimized and opacity is 0. I want to change the opacity to 1 the use has clicked the three dots button to expand the bar. like so:
if (ApplicationBar.Mode == ApplicationBarMode.Default)
{
ApplicationBar.Opacity = 1;
}
else {
ApplicationBar.Opacity = 0;
}
You cannot compare ApplicationBar.Mode as it doesn't change when you click ellipsis (those three dots). Thought you can surely try subscribe to StateChanged event to do something like this :
// in constructor
ApplicationBar.StateChanged+=ApplicationBar_StateChanged;
private void ApplicationBar_StateChanged(object sender, ApplicationBarStateChangedEventArgs e)
{
if (e.IsMenuVisible) ApplicationBar.Opacity = 1;
else ApplicationBar.Opacity = 0;
}
But to make it work your ApplicationBar must have MenuItems.

highlighting the button that fired an event

Consider a SilverLight project that has 31 hyperlinkbuttons. Those represent the days of the month. I'm using this code to highlight the hyperlinkbutton that respresent today's day.
var daynumberHyperButton = this.FindName("Day" + DateTime.Today.Day) as HyperlinkButton;
//Highlighting the day of the month
if (daynumberHyperButton != null)
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Gray);
}
Then if I click on this highlighted hyperlinkbutton, it will open a childwindow to write some report.
private void dayHyperLink_Click(object sender, RoutedEventArgs e)
{
//This will initite and show the report window
ReportWindow rapport = new ReportWindow();
rapport.Closed += new EventHandler(rapport_Closed);
rapport.Show();
}
When I close the childwindows by clicking the OK button, it changes the color of the hyperlinkbutton that was highlighted (todays day) because I'm using this code to do that:-
private void rapport_Closed(object sender, EventArgs e)
{
ReportWindow rapport = (ReportWindow)sender;
var daynumberHyperButton = this.FindName("Day" + DateTime.Today.Day) as HyperlinkButton;
if (rapport.UsersValue == "Röd" && rapport.DialogResult==true)
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Red);
}
else if (rapport.UsersValue == "Gul")
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Yellow);
}
else
{
daynumberHyperButton.Background = new SolidColorBrush(Colors.Green);
}
}
But if I click on any other hyperlinkbutton that is not highlighted, it still only change the color of the highlighted hyperlinkbutton. I know this because my rapport_Closed event has:
var daynumberHyperButton = this.FindName("Day" + DateTime.Today.Day) as HyperlinkButton;
How can I change the above code, which is part of my rapport_Closed event, so that it changes the color of the event firing (the one that opens the childwindow) hyperlinkbutton, no matter which hyperlinkbuttonis the one that fires the event?
Ok now i can say i've done it. Here is what i did if anyone have a similar problem.
In may Home.xaml.cs, i added a public property like this:-
public HyperlinkButton dayHyperLink { get; set; }
To the Click event i added this code:-
dayHyperLink = (HyperlinkButton)sender;
To the rapport_Closing event i changed the if statment to the code below:-
if (rapport.UsersValue == "Röd" && rapport.DialogResult == true)
{
dayHyperLink.Background = new SolidColorBrush(Colors.Red);
}
This made me feel happy ;)

Categories

Resources