问题
I have created a popup and have some buttons over it and I am not able to select those and nor does the XBox default selection box appears around the button.
<Grid>
<StackPanel HorizontalAlignment="Center" Margin="0,50,0,0">
<Button Content="Show Popup Flyout" Click="ShowFlyoutPopup" />
</StackPanel>
<Popup x:Name="logincontrol1" IsOpen="False" IsLightDismissEnabled="True">
<Popup.ChildTransitions>
<TransitionCollection>
<PaneThemeTransition />
</TransitionCollection>
</Popup.ChildTransitions>
<StackPanel Orientation="Vertical" Background="#313131" Width="1000" Height="500" x:Name="pop" Margin="0,100,0,0" >
<StackPanel>
<GridView ItemsSource="{Binding GamesList}" >
<GridView.ItemTemplate>
<DataTemplate x:Name="testtemp">
<Button x:Name="SwapBtn" Command="{Binding TestClick,Mode=OneWay}">
<StackPanel Width="202" VerticalAlignment="Top">
<Image Source="{Binding ImageUrl}" Height="324"/>
<StackPanel Background="Black" Padding="19,9,0,0">
<TextBlock FontWeight="Semibold" TextTrimming="CharacterEllipsis" FontFamily="Segoe Pro" Foreground="White" TextAlignment="Left" FontSize="24" Text="{Binding Title}" TextWrapping="Wrap" Height="65"/>
<TextBlock FontFamily="Segoe Pro" Foreground="White" TextAlignment="Left" FontSize="16" Text="{Binding Price}" Margin="0,48,0,0"/>
</StackPanel>
</StackPanel>
</Button>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</StackPanel>
</StackPanel>
</Popup>
</Grid>
Code behind: Its like this and I am not able to get the focus on my popup. I thought it should work automatically.
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void ShowFlyoutPopup(object sender, RoutedEventArgs e)
{
if (!logincontrol1.IsOpen)
{
RootPopupBorder.Width = 646;
logincontrol1.HorizontalOffset = Window.Current.Bounds.Width - 1485;
logincontrol1.VerticalOffset = Window.Current.Bounds.Height - 840;
logincontrol1.IsOpen = true;
}
}
}
}
The focus is not there in the popup and I cannot interact with it using the gamepad.
回答1:
I'd handle the opened event of the popup,
<Popup x:Name="logincontrol1" IsOpen="False" IsLightDismissEnabled="True" Opened="Logincontrol1_OnOpened">
Controls inside your popup datatemplate is not gonna be on the visual tree of your page/view it will kind of have it's own visual tree, for this you can use VisualTreeHelper to get the control inside the popup to set the focus.
private void Logincontrol1_OnOpened(object sender, object e)
{
Popup popup = sender as Popup;
if (popup != null)
{
Button button = FindElementInVisualTree<Button>(mygrid);
button?.Focus(FocusState.Programmatic);
}
}
private T FindElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0) return null;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
return (T)child;
else
{
var result = FindElementInVisualTree<T>(child);
if (result != null)
return result;
}
}
return null;
}
回答2:
You can force focus on one of the Popup
's controls:
logincontrol1.IsOpen = true;
loginclick.Focus(FocusState.Programmatic);
来源:https://stackoverflow.com/questions/52312441/i-cannot-get-the-selection-box-in-my-xbox-uwp-app-popup