英文:
NavigationView and AutoSuggestbox not suggesting NavigationViewItems UWP
问题
我遇到了一个与匹配导航视图项的“Content”属性值的问题。我的应用程序相当大,所以我为了这篇帖子制作了一个示例视图。
这是我的XAML视图代码:
<Page
x:Class="testowa.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:testowa"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:rg="using:SourceChord.ResponsiveGrid"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
muxc:BackdropMaterial.ApplyToRootOrPageBackground="True">
<Page.Resources>
<!--This top margin is the height of the custom TitleBar-->
<Thickness x:Key="NavigationViewContentMargin">0,48,0,0</Thickness>
</Page.Resources>
<Grid>
<Border x:Name="AppTitleBar"
IsHitTestVisible="True"
VerticalAlignment="Top"
Background="Transparent"
Height="40"
Canvas.ZIndex="1"
Margin="48,8,0,0">
<StackPanel Orientation="Horizontal">
<Image x:Name="AppFontIcon"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Source="Assets/Square44x44Logo.png"
Width="16"
Height="16"/>
<TextBlock x:Name="AppTitle"
Text="Aplikacja Szpitalna"
VerticalAlignment="Center"
Margin="12, 0, 0, 0"
Style="{StaticResource CaptionTextBlockStyle}" />
</StackPanel>
</Border>
<muxc:NavigationView x:Name="NavigationViewControl"
IsTitleBarAutoPaddingEnabled="False"
IsBackButtonVisible="Visible"
DisplayModeChanged="NavigationViewControl_DisplayModeChanged"
Canvas.ZIndex="0">
<muxc:NavigationView.Header>
<Grid HorizontalAlignment="Left" Padding="0" Margin="6,0,0,0">
<TextBlock Text="Dashboard" HorizontalAlignment="Center" FontSize="20" />
</Grid>
</muxc:NavigationView.Header>
<muxc:NavigationView.AutoSuggestBox>
<AutoSuggestBox
PlaceholderText="Wyszukaj"
QueryIcon="Find"
x:Name="controlsSearchBox"
TextChanged="ControlsSearchBox_TextChanged"
VerticalAlignment="Center"
x:FieldModifier="public"
MinWidth="200"
KeyboardAcceleratorPlacementMode="Hidden">
</AutoSuggestBox>
</muxc:NavigationView.AutoSuggestBox>
<muxc:NavigationView.MenuItems>
<muxc:NavigationViewItemSeparator />
<muxc:NavigationViewItem Icon="Home" Content="Menu Główne"/>
<muxc:NavigationViewItemSeparator />
<!--Sekcja techniczna-->
<muxc:NavigationViewItemHeader Content="Techniczne" Margin="0,20,0,0"/>
<muxc:NavigationViewItem Icon="List" Content="System Kolejkowy"/>
<muxc:NavigationViewItem Icon="FourBars" Content="Networking">
<muxc:NavigationViewItem.MenuItems>
<muxc:NavigationViewItem Content="Serwery" Icon="AllApps"/>
<muxc:NavigationViewItem Content="Urządzenia aktywne" Icon="Manage"/>
<muxc:NavigationViewItem Content="Punkty PPD" Icon="Map"/>
<muxc:NavigationViewItem Content="Adresy IP" Icon="GoToStart"/>
<muxc:NavigationViewItem Content="Dostęp zdalny" Icon="Globe"/>
<muxc:NavigationViewItem Content="Skaner ARP" Icon="Find"/>
</muxc:NavigationViewItem.MenuItems>
</muxc:NavigationViewItem>
</muxc:NavigationView.MenuItems>
</muxc:NavigationView>
</Grid>
</Page>
这是绑定到AutoSuggestBox的事件方法:
private void ControlsSearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
// Pobierz tekst z elementu AutoSuggestBox
string searchText = sender.Text;
// Filtruj elementy menu nawigacyjnego za pomocą pobranego tekstu
var filteredMenuItems = NavigationViewControl.MenuItems
.OfType<NavigationViewItem>()
.Where(item => item.Content.ToString().ToLowerInvariant().Contains(searchText.ToLowerInvariant()))
.ToList();
// Ustaw wynik filtrowania jako elementy sugestii dla elementu AutoSuggestBox
sender.ItemsSource = filteredMenuItems;
}
什么都没有发生,Debug.WriteLine显示filteredMenuItems中有0个元素。我已经寻找解决方案几天了。检查了Learn.Microsoft和示例GitHub项目。我期望代码显示与Content匹配的NavigationViewItems。
英文:
I've stuck on the problem with matching phrases with Property value "Content" of NavigationViewItem. My app is pretty large, so I made a sample view for the sake of this post.
This is my XAML View Code
<Page
x:Class="testowa.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:testowa"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:rg="using:SourceChord.ResponsiveGrid"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
muxc:BackdropMaterial.ApplyToRootOrPageBackground="True">
<Page.Resources>
<!--This top margin is the height of the custom TitleBar-->
<Thickness x:Key="NavigationViewContentMargin">0,48,0,0</Thickness>
</Page.Resources>
<Grid>
<Border x:Name="AppTitleBar"
IsHitTestVisible="True"
VerticalAlignment="Top"
Background="Transparent"
Height="40"
Canvas.ZIndex="1"
Margin="48,8,0,0">
<StackPanel Orientation="Horizontal">
<Image x:Name="AppFontIcon"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Source="Assets/Square44x44Logo.png"
Width="16"
Height="16"/>
<TextBlock x:Name="AppTitle"
Text="Aplikacja Szpitalna"
VerticalAlignment="Center"
Margin="12, 0, 0, 0"
Style="{StaticResource CaptionTextBlockStyle}" />
</StackPanel>
</Border>
<muxc:NavigationView x:Name="NavigationViewControl"
IsTitleBarAutoPaddingEnabled="False"
IsBackButtonVisible="Visible"
DisplayModeChanged="NavigationViewControl_DisplayModeChanged"
Canvas.ZIndex="0">
<muxc:NavigationView.Header>
<Grid HorizontalAlignment="Left" Padding="0" Margin="6,0,0,0">
<TextBlock Text="Dashboard" HorizontalAlignment="Center" FontSize="20" />
</Grid>
</muxc:NavigationView.Header>
<muxc:NavigationView.AutoSuggestBox>
<AutoSuggestBox
PlaceholderText="Wyszukaj"
QueryIcon="Find"
x:Name="controlsSearchBox"
TextChanged="ControlsSearchBox_TextChanged"
VerticalAlignment="Center"
x:FieldModifier="public"
MinWidth="200"
KeyboardAcceleratorPlacementMode="Hidden">
</AutoSuggestBox>
</muxc:NavigationView.AutoSuggestBox>
<muxc:NavigationView.MenuItems>
<muxc:NavigationViewItemSeparator />
<muxc:NavigationViewItem Icon="Home" Content="Menu Główne"/>
<muxc:NavigationViewItemSeparator />
<!--Sekcja techniczna-->
<muxc:NavigationViewItemHeader Content="Techniczne" Margin="0,20,0,0"/>
<muxc:NavigationViewItem Icon="List" Content="System Kolejkowy"/>
<muxc:NavigationViewItem Icon="FourBars" Content="Networking">
<muxc:NavigationViewItem.MenuItems>
<muxc:NavigationViewItem Content="Serwery" Icon="AllApps"/>
<muxc:NavigationViewItem Content="Urządzenia aktywne" Icon="Manage"/>
<muxc:NavigationViewItem Content="Punkty PPD" Icon="Map"/>
<muxc:NavigationViewItem Content="Adresy IP" Icon="GoToStart"/>
<muxc:NavigationViewItem Content="Dostęp zdalny" Icon="Globe"/>
<muxc:NavigationViewItem Content="Skaner ARP" Icon="Find"/>
</muxc:NavigationViewItem.MenuItems>
</muxc:NavigationViewItem>
</muxc:NavigationView.MenuItems>
</muxc:NavigationView>
</Grid>
</Page>
This is Event Method binded to AutoSuggestBox
private void ControlsSearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
// Pobierz tekst z elementu AutoSuggestBox
string searchText = sender.Text;
// Filtruj elementy menu nawigacyjnego za pomocą pobranego tekstu
var filteredMenuItems = NavigationViewControl.MenuItems
.OfType<NavigationViewItem>()
.Where(item => item.Content.ToString().ToLowerInvariant().Contains(searchText.ToLowerInvariant()))
.ToList();
// Ustaw wynik filtrowania jako elementy sugestii dla elementu AutoSuggestBox
sender.ItemsSource = filteredMenuItems;
}
Nothing happens, Debug.WriteLine shows 0 elements in filteredMenuItems. I've been looking the solution for days. Checked Learn.Microsoft, sample Github projects.
I'm expecting the code shows NavigationViewItems which match the phrase with Content
答案1
得分: 0
以下是翻译好的部分:
在NavigationViewControl.MenuItems
中不仅有NavigationViewItem
,还有NavigationViewItemSeparator
和NavigationViewItemHeader
。
请尝试以下代码:
public List<string> CheckItems(List<object> sourceList, List<string> resultList, string searchText)
{
foreach (var item in sourceList)
{
if (item.GetType() == typeof(MUXC.NavigationViewItem))
{
// 获取一级项
MUXC.NavigationViewItem viewItem = item as MUXC.NavigationViewItem;
// 检查是否包含二级项
List<object> level2Items = viewItem.MenuItems.ToList();
if (level2Items.Count > 0)
{
CheckItems(level2Items, resultList, searchText);
}
else
{
// 这是一级项
var str = viewItem.Content.ToString().ToLowerInvariant();
if (str.Contains(searchText))
{
resultList.Add(viewItem.Content.ToString());
}
}
}
}
return resultList;
}
private void controlsSearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
string searchText = sender.Text;
List<string> resultList = new List<string>();
List<object> itemlist = NavigationViewControl.MenuItems.ToList();
CheckItems(itemlist, resultList, searchText);
sender.ItemsSource = resultList;
}
请注意,代码中的MUXC.NavigationViewItem
和NavigationViewControl.MenuItems
是特定的类和属性,未提供上下文信息,翻译时保留了原文。
英文:
There are not only NavigationViewItem
in the NavigationViewControl.MenuItems
, but also NavigationViewItemSeparator
and NavigationViewItemHeader
in it.
Please try the following code:
public List<string> CheckItems( List<object> sourceList, List<string> resultList, string searchText)
{
foreach (var item in sourceList)
{
if (item.GetType() == typeof(MUXC.NavigationViewItem))
{
// get first level item
MUXC.NavigationViewItem viewItem = item as MUXC.NavigationViewItem;
//check if contains second level items
List<object> level2Items = viewItem.MenuItems.ToList();
if (level2Items.Count > 0)
{
CheckItems(level2Items,resultList,searchText);
}
else
{
// this is the first level item
var str = viewItem.Content.ToString().ToLowerInvariant();
if (str.Contains(searchText))
{
resultList.Add(viewItem.Content.ToString());
}
}
}
}
return resultList;
}
private void controlsSearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
string searchText = sender.Text;
List<string> resultList = new List<string>();
List<object> itemlist = NavigationViewControl.MenuItems.ToList();
CheckItems(itemlist, resultList, searchText);
sender.ItemsSource = resultList;
}
答案2
得分: 0
让我给您提供另一个选项,可以切换每个项目的“可见性”。
private void ControlsSearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
string filter = sender.Text.ToLowerInvariant();
foreach (object item in this.NavigationViewControl.MenuItems)
{
MakeVisibleItemsThatMatchFilter(parent: null, child: item, filter);
}
}
private void MakeVisibleItemsThatMatchFilter(NavigationViewItem? parent, object child, string filter)
{
if (child is NavigationViewItem childNavigationViewItem)
{
if (childNavigationViewItem.Content is string content &&
content.ToLowerInvariant().Contains(filter) is true)
{
childNavigationViewItem.Visibility = Visibility.Visible;
if (parent is not null)
{
parent.Visibility = Visibility.Visible;
parent.IsExpanded = true;
}
}
else
{
childNavigationViewItem.Visibility = Visibility.Collapsed;
}
foreach (object grandChild in childNavigationViewItem.MenuItems)
{
MakeVisibleItemsThatMatchFilter(childNavigationViewItem, grandChild, filter);
}
}
else if (child is UIElement childUIElement)
{
childUIElement.Visibility = string.IsNullOrEmpty(filter) is true
? Visibility.Visible
: Visibility.Collapsed;
}
}
英文:
Let me give you another option that switches the Visibility
of each item.
private void ControlsSearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
string filter = sender.Text.ToLowerInvariant();
foreach (object item in this.NavigationViewControl.MenuItems)
{
MakeVisibleItemsThatMatchFilter(parent: null, child: item, filter);
}
}
private void MakeVisibleItemsThatMatchFilter(NavigationViewItem? parent, object child, string filter)
{
if (child is NavigationViewItem childNavigationViewItem)
{
if (childNavigationViewItem.Content is string content &&
content.ToLowerInvariant().Contains(filter) is true)
{
childNavigationViewItem.Visibility = Visibility.Visible;
if (parent is not null)
{
parent.Visibility = Visibility.Visible;
parent.IsExpanded = true;
}
}
else
{
childNavigationViewItem.Visibility = Visibility.Collapsed;
}
foreach (object grandChild in childNavigationViewItem.MenuItems)
{
MakeVisibleItemsThatMatchFilter(childNavigationViewItem, grandChild, filter);
}
}
else if (child is UIElement childUIElement)
{
childUIElement.Visibility = string.IsNullOrEmpty(filter) is true
? Visibility.Visible
: Visibility.Collapsed;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论