0

I want my button color to change when xbox select box is on the button. How can I achieve that? I haven't got an event on focus in Xbox UWP app.

Matthias
  • 4,481
  • 12
  • 45
  • 84
sadik
  • 107
  • 10

1 Answers1

0

you need a GotFocus and LostFocus events on that button you are trying to do this.

<Button x:Name="MyButton" GotFocus="ButtonGotFocus" LostFocus="ButtonLostFocus"/>

and in the code behind you can change the background color accordingly. you can also optionally check the DeviceFamily first if you want the behavior only on xbox.

private void GotFocus(object sender, object args)
{
    if(AnalyticsVersionInfo.DeviceFamily == "Windows.Xbox")
    {
        //change the color when the button gets focus
        MyButton.BackgroundColor = new SolidColorBrush(Colors.Blue);
    }
}

private void LostFocus(object sender, object args)
{
    if(AnalyticsVersionInfo.DeviceFamily == "Windows.Xbox")
    {
        //change the color when the button looses focus
        MyButton.BackgroundColor = new SolidColorBrush(Colors.Green);
    }
}

more on DeviceFamily property : https://learn.microsoft.com/en-us/uwp/api/windows.system.profile.analyticsversioninfo.devicefamily#Windows_System_Profile_AnalyticsVersionInfo_DeviceFamily

Update

if you want same effect on all or multiple buttons, just assign the events on every button you want the affect on like so :

<Button x:Name="MyButton" GotFocus="ButtonGotFocus" LostFocus="ButtonLostFocus"/>
<Button x:Name="MyButton2" GotFocus="ButtonGotFocus" LostFocus="ButtonLostFocus"/>

and in the backend just replce MyButton with (sender as Button)

(sender as Button).BackgroundColor = new SolidColorBrush(Colors.Green);
Muhammad Touseef
  • 4,357
  • 4
  • 31
  • 75
  • Thanks, its working. Anyway we could make this as a global function so that this can be used in the whole application? – sadik Sep 10 '18 at 12:55
  • yes , just assign the gotfocus and lostfocus events to every button in xaml. this way every button will get this behaviour and u have to write the backend events only once, but instead of using MyButton, just use sender as Button, see the updated answer – Muhammad Touseef Sep 10 '18 at 12:58