gpt4 book ai didi

c# - UWP 防止在 AutosuggestBox 上自动聚焦

转载 作者:太空宇宙 更新时间:2023-11-03 22:56:40 29 4
gpt4 key购买 nike

我在 ContentDialog 中有一个 AutosuggestBox,如下所示:

<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 的 GotFocus 事件中设置了 IsSuggestionListOpen = True,建议列表会在内容对话框打开后立即打开。我想停止此操作并仅在用户点击/点击 AutosuggestBOx

后打开 SuggestionList

要补充这个问题,当 AutosuggestBox 已经获得焦点并且我点击外面的某个地方时,它不会失去焦点。

最佳答案

尝试在 AutoSuggestBox 和您的 ListView 之间切换顺序,然后将 ListViewIsTapStop 设置为正确

<ListView Grid.Row="3" IsTabStop="True" ... />
<AutoSuggestBox Grid.Row="1" ... />

更新

ContentDialog 的默认样式中有一个 Grid,它有一个默认的 Padding(即 ContentDialogPadding)和它以某种方式阻止了 LostFocus 的发生。

您可以通过删除此 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>

上面的代码大部分来自默认样式,除了我做了两处改动——

  1. 我删除了 DialogSpace 上的 Padding
  2. 我给 CommandSpace Margin 24,0,24,24

最后,我将 24,18,24,0 的默认 Padding 添加到您的 top 级别 GridContentDialog 中。


更新2

如果您想关闭 AutoSuggestBoxPopup,您必须手动执行。在其 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);
}
}
}

关于c# - UWP 防止在 AutosuggestBox 上自动聚焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45073552/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com