AutosuggestBox
内有ContentDialog
,如下所示:
<ContentDialog x:Name="BrowseBySourceDialog"
Grid.Column="1"
PrimaryButtonText="{x:Bind ViewModel._BTN_ok}"
PrimaryButtonClick="BrowseBySourceDialog_PrimaryButtonClick"
SecondaryButtonClick="Dialog_SecondaryButtonClick"
SecondaryButtonText="{x:Bind ViewModel._BTN_close}"
Opened="BrowseBySourceDialog_Opened"
GotFocus="BrowseBySourceDialog_GotFocus"
Style="{StaticResource ContentDialogStyles}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Border Background="#1A503E"
MinHeight="40"
Grid.Row="0">
<TextBlock Text="{x:Bind ViewModel._HEADER_BROWSE_sourceOrganization}"
Style="{StaticResource DialogTextBlockStyle}" />
</Border>
<ListView Name="BrowseBySourceTopResultsListView" IsTabStop="True"
x:FieldModifier="public"
Grid.Row="3"
SelectionMode="Single"
IsItemClickEnabled="True"
ItemClick="FeaturedResultsSourceListView_ItemClick"
MaxHeight="500"
ItemsSource="{x:Bind BrowseBySourceList}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:DataType="viewModels:BrowseByStarViewModel">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Source="{x:Bind icon}"
Width="40"
Height="40"
Margin="10"/>
<TextBlock Text="{x:Bind read}"
Grid.Column="1"
TextAlignment="Center"
VerticalAlignment="Center"
Margin="20, 0, 0, 0"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<AutoSuggestBox Grid.Row="1"
Name="asbBrowseBySource"
TextMemberPath="read"
PlaceholderText="{x:Bind ViewModel._TIPS_source_select}"
LostFocus="asbBrowseBySource_LostFocus"
GotFocus="asbBrowseBySource_GotFocus"
TextChanged="asbBrowseBySource_TextChanged">
<AutoSuggestBox.ItemTemplate>
<DataTemplate x:DataType="response:ExploreApiResponseItem">
<TextBlock Text="{x:Bind read}"
Margin="5"/>
</DataTemplate>
</AutoSuggestBox.ItemTemplate>
</AutoSuggestBox>
<ComboBox x:Name="BrowseBySourceComboBox"
LostFocus="BrowseBySourceComboBox_LostFocus"
SelectionChanged="BrowseBySourceComboBox_SelectionChanged"
Grid.Row="2"
PlaceholderText="{x:Bind ViewModel._TIPS_source_select}"
IsTextSearchEnabled="True"
Style="{StaticResource ComboBoxStyle}"
ItemsSource="{x:Bind BrowseBySourceComboBoxList, Mode=OneWay}">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="response:ExploreApiResponseItem">
<TextBlock Text="{x:Bind read}"
Margin="5"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
问题在于,只要我打开ContentDialog
,我的AutosuggestBox
就会自动关注。因为我在AutosuggestBox的IsSuggestionListOpen = True
事件中设置了GotFocus
,所以建议列表会在打开内容对话框后立即打开。我想停止此操作并仅在用户点击/点击AutosuggestBOx
为了补充这个问题,当AutosuggestBox已经集中并且我点击外面的某个地方时,它并没有失去它的重点。
答案 0 :(得分:2)
尝试在AutoSuggestBox
和ListView
之间切换顺序,然后将ListView
&#39; IsTapStop
设置为True
。
<ListView Grid.Row="3" IsTabStop="True" ... />
<AutoSuggestBox Grid.Row="1" ... />
Grid
内ContentDialog
的默认样式Padding
有默认ContentDialogPadding
(即LostFocus
),它会以某种方式阻止{{1}发生的事情。
您可以通过删除此Padding
并手动隔离其中的元素来解决此问题。
<Style TargetType="ContentDialog">
<Setter Property="Foreground"
Value="{ThemeResource ContentDialogForeground}" />
<Setter Property="Background"
Value="{ThemeResource ContentDialogBackground}" />
<Setter Property="BorderBrush"
Value="{ThemeResource ContentDialogBorderBrush}" />
<Setter Property="HorizontalAlignment"
Value="Center" />
<Setter Property="VerticalAlignment"
Value="Top" />
<Setter Property="IsTabStop"
Value="False" />
<Setter Property="MaxHeight"
Value="{ThemeResource ContentDialogMaxHeight}" />
<Setter Property="MaxWidth"
Value="{ThemeResource ContentDialogMaxWidth}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentDialog">
<Border x:Name="Container">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ButtonsVisibilityStates">
<VisualState x:Name="AllVisible" />
<VisualState x:Name="NoneVisible">
<VisualState.Setters>
<Setter Target="PrimaryButton.Visibility"
Value="Collapsed" />
<Setter Target="SecondaryButton.Visibility"
Value="Collapsed" />
<Setter Target="CloseButton.Visibility"
Value="Collapsed" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="PrimaryVisible">
<VisualState.Setters>
<Setter Target="PrimaryButton.(Grid.Column)"
Value="2" />
<Setter Target="PrimaryButton.(Grid.ColumnSpan)"
Value="2" />
<Setter Target="PrimaryButton.Margin"
Value="2,0,0,0" />
<Setter Target="SecondaryButton.Visibility"
Value="Collapsed" />
<Setter Target="CloseButton.Visibility"
Value="Collapsed" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="SecondaryVisible">
<VisualState.Setters>
<Setter Target="SecondaryButton.(Grid.Column)"
Value="2" />
<Setter Target="SecondaryButton.(Grid.ColumnSpan)"
Value="2" />
<Setter Target="SecondaryButton.Margin"
Value="2,0,0,0" />
<Setter Target="PrimaryButton.Visibility"
Value="Collapsed" />
<Setter Target="CloseButton.Visibility"
Value="Collapsed" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="CloseVisible">
<VisualState.Setters>
<Setter Target="CloseButton.(Grid.Column)"
Value="2" />
<Setter Target="CloseButton.(Grid.ColumnSpan)"
Value="2" />
<Setter Target="CloseButton.Margin"
Value="2,0,0,0" />
<Setter Target="PrimaryButton.Visibility"
Value="Collapsed" />
<Setter Target="SecondaryButton.Visibility"
Value="Collapsed" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="PrimaryAndSecondaryVisible">
<VisualState.Setters>
<Setter Target="PrimaryButton.(Grid.ColumnSpan)"
Value="2" />
<Setter Target="SecondaryButton.(Grid.Column)"
Value="2" />
<Setter Target="SecondaryButton.(Grid.ColumnSpan)"
Value="2" />
<Setter Target="SecondaryButton.Margin"
Value="2,0,0,0" />
<Setter Target="CloseButton.Visibility"
Value="Collapsed" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="PrimaryAndCloseVisible">
<VisualState.Setters>
<Setter Target="PrimaryButton.(Grid.ColumnSpan)"
Value="2" />
<Setter Target="CloseButton.(Grid.Column)"
Value="2" />
<Setter Target="CloseButton.(Grid.ColumnSpan)"
Value="2" />
<Setter Target="CloseButton.Margin"
Value="2,0,0,0" />
<Setter Target="SecondaryButton.Visibility"
Value="Collapsed" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="SecondaryAndCloseVisible">
<VisualState.Setters>
<Setter Target="SecondaryButton.(Grid.Column)"
Value="0" />
<Setter Target="SecondaryButton.(Grid.ColumnSpan)"
Value="2" />
<Setter Target="SecondaryButton.Margin"
Value="0,0,2,0" />
<Setter Target="CloseButton.(Grid.Column)"
Value="2" />
<Setter Target="CloseButton.(Grid.ColumnSpan)"
Value="2" />
<Setter Target="CloseButton.Margin"
Value="2,0,0,0" />
<Setter Target="PrimaryButton.Visibility"
Value="Collapsed" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="DefaultButtonStates">
<VisualState x:Name="NoDefaultButton" />
<VisualState x:Name="PrimaryAsDefaultButton">
<VisualState.Setters>
<Setter Target="PrimaryButton.Style"
Value="{StaticResource AccentButtonStyle}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="SecondaryAsDefaultButton">
<VisualState.Setters>
<Setter Target="SecondaryButton.Style"
Value="{StaticResource AccentButtonStyle}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="CloseAsDefaultButton">
<VisualState.Setters>
<Setter Target="CloseButton.Style"
Value="{StaticResource AccentButtonStyle}" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Border x:Name="BackgroundElement"
Background="{TemplateBinding Background}"
FlowDirection="{TemplateBinding FlowDirection}"
BorderThickness="{ThemeResource ContentDialogBorderWidth}"
BorderBrush="{TemplateBinding BorderBrush}"
MaxWidth="{TemplateBinding MaxWidth}"
MaxHeight="{TemplateBinding MaxHeight}"
MinWidth="{ThemeResource ContentDialogMinWidth}"
MinHeight="{ThemeResource ContentDialogMinHeight}">
<Grid x:Name="DialogSpace"
VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ScrollViewer x:Name="ContentScrollViewer"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Disabled"
ZoomMode="Disabled"
Margin="{ThemeResource ContentDialogContentScrollViewerMargin}"
IsTabStop="False">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentControl x:Name="Title"
Margin="{ThemeResource ContentDialogTitleMargin}"
Content="{TemplateBinding Title}"
ContentTemplate="{TemplateBinding TitleTemplate}"
FontSize="20"
FontFamily="XamlAutoFontFamily"
FontWeight="Normal"
Foreground="{TemplateBinding Foreground}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
IsTabStop="False">
<ContentControl.Template>
<ControlTemplate TargetType="ContentControl">
<ContentPresenter Content="{TemplateBinding Content}"
MaxLines="2"
TextWrapping="Wrap"
ContentTemplate="{TemplateBinding ContentTemplate}"
Margin="{TemplateBinding Padding}"
ContentTransitions="{TemplateBinding ContentTransitions}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
<ContentPresenter x:Name="Content"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
FontSize="{ThemeResource ControlContentThemeFontSize}"
FontFamily="{ThemeResource ContentControlThemeFontFamily}"
Margin="{ThemeResource ContentDialogContentMargin}"
Foreground="{TemplateBinding Foreground}"
Grid.Row="1"
TextWrapping="Wrap" />
</Grid>
</ScrollViewer>
<Grid x:Name="CommandSpace"
Margin="24,0,24,24"
Grid.Row="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Bottom"
XYFocusKeyboardNavigation="Enabled">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="0.5*" />
<ColumnDefinition Width="0.5*" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button x:Name="PrimaryButton"
Content="{TemplateBinding PrimaryButtonText}"
IsEnabled="{TemplateBinding IsPrimaryButtonEnabled}"
Style="{TemplateBinding PrimaryButtonStyle}"
ElementSoundMode="FocusOnly"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="0,0,2,0"
Grid.Column="0" />
<Button x:Name="SecondaryButton"
Content="{TemplateBinding SecondaryButtonText}"
IsEnabled="{TemplateBinding IsSecondaryButtonEnabled}"
Style="{TemplateBinding SecondaryButtonStyle}"
ElementSoundMode="FocusOnly"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="2,0,2,0"
Grid.Column="1"
Grid.ColumnSpan="2" />
<Button x:Name="CloseButton"
Content="{TemplateBinding CloseButtonText}"
Style="{TemplateBinding CloseButtonStyle}"
ElementSoundMode="FocusOnly"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="2,0,0,0"
Grid.Column="3" />
</Grid>
</Grid>
</Border>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
上面的代码主要来自默认样式,除了我做的两个更改 -
Padding
上的DialogSpace
。 CommandSpace
Margin
24,0,24,24
。最后,我在Padding
内的顶部级24,18,24,0
添加了Grid
的默认ContentDialog
。
如果您要关闭AutoSuggestBox
&#39; Popup
,则必须手动执行此操作。在其GotFocus
处理程序中,使用VisualTreeHelper
找到背景矩形,并与其Tapped
事件挂钩,因此无论何时点击它,您只需手动设置IsSuggestionListOpen
到false
。您还必须将焦点设置到另一个控件上。
var box = (AutoSuggestBox)sender;
box.IsSuggestionListOpen = true;
var popups = VisualTreeHelper.GetOpenPopups(Window.Current);
foreach (var popup in popups)
{
if (popup.Child is Rectangle bg)
{
bg.Tapped += Bg_Tapped;
void Bg_Tapped(object s, TappedRoutedEventArgs args)
{
bg.Tapped -= Bg_Tapped;
box.IsSuggestionListOpen = false;
BrowseBySourceTopResultsListView.Focus(FocusState.Programmatic);
}
}
}
答案 1 :(得分:1)
这是我的代码......是的它很脏但是有效:) 此处理程序附加到内容对话框的“Opend”事件。
private async void manageBoardsDialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args)
{
// at first, disable the autosuggest control
autoSuggestBox.IsEnabled = false;
// do some async task
// Set focus to another control
itemGridView.Focus(FocusState.Programmatic);
// re-enable it
autoSuggestBox.IsEnabled = true;
}
答案 2 :(得分:0)
UWP将重点放在Visual Tree上的第一个可编辑控件上。因此,您可以添加可编辑控件,确保它在UI上不可见,即使它仍在UI上(不启用/禁用Visibility
),然后在“手动加载”上将焦点设置为该控件以确保它得到了关注。
这是我之前遇到同样问题时所做的事情。
在ContentDialog
<TextBox x:Name="FocusedTextBox" Width="0" Height="0" Opacity="0" Margin="-5000,0,0,0" />
现在在你的Loaded
中说出来
FocusedTextBox.Focus(FocusState.Programmatic);