Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing AppBarButton Icon

In my Windows 10 universal application I've Got a AppBarButton in a page:

<AppBarButton x:Name="PinBtn" Icon="Pin" Click="PinBtn_Click"/>

I want to check if Icon="Pin" then UnPin it and vice versa.

So I tried to code like this:

  private void PinBtn_Click(object sender,RoutedEventArgs e)
    {
        if(PinBtn.Icon == new SymbolIcon((Symbol.Pin)))
        {
            PinBtn.Icon = new SymbolIcon(Symbol.UnPin);
        }
        else {
            PinBtn.Icon = new SymbolIcon(Symbol.Pin);
        }
    }

Problem : (PinBtn.Icon == new SymbolIcon((Symbol.UnPin))) always return FALSE

What's the problem and fix for it?

like image 747
Shahriar Avatar asked Aug 10 '15 22:08

Shahriar


1 Answers

It's not going to work 'cause Icon is a reference type. It will never be equal to a new instance of SymbolIcon.

You should be using a AppBarToggleButton in this case instead and subscribe to the Checked and Unchecked events.

<AppBarToggleButton x:Name="PinToggle" Icon="Pin" Checked="PinToggle_Checked" Unchecked="PinToggle_Unchecked" />

private void PinToggle_Checked(object sender, RoutedEventArgs e)
{
    PinToggle.Icon = new SymbolIcon(Symbol.UnPin);
}

private void PinToggle_Unchecked(object sender, RoutedEventArgs e)
{
    PinToggle.Icon = new SymbolIcon(Symbol.Pin);
}
like image 197
Justin XL Avatar answered Oct 13 '22 21:10

Justin XL