NavigationView 和 AutoSuggestBox 未建议 NavigationViewItem UWP。

huangapple go评论82阅读模式
英文:

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

&lt;Page
x:Class=&quot;testowa.MainPage&quot;
xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
xmlns:local=&quot;using:testowa&quot;
xmlns:muxc=&quot;using:Microsoft.UI.Xaml.Controls&quot;
xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
xmlns:rg=&quot;using:SourceChord.ResponsiveGrid&quot;
mc:Ignorable=&quot;d&quot;
Background=&quot;{ThemeResource ApplicationPageBackgroundThemeBrush}&quot;
muxc:BackdropMaterial.ApplyToRootOrPageBackground=&quot;True&quot;&gt;
&lt;Page.Resources&gt;
&lt;!--This top margin is the height of the custom TitleBar--&gt;
&lt;Thickness x:Key=&quot;NavigationViewContentMargin&quot;&gt;0,48,0,0&lt;/Thickness&gt;
&lt;/Page.Resources&gt;
&lt;Grid&gt;
&lt;Border x:Name=&quot;AppTitleBar&quot;
IsHitTestVisible=&quot;True&quot;
VerticalAlignment=&quot;Top&quot;
Background=&quot;Transparent&quot;
Height=&quot;40&quot;
Canvas.ZIndex=&quot;1&quot; 
Margin=&quot;48,8,0,0&quot;&gt;
&lt;StackPanel Orientation=&quot;Horizontal&quot;&gt;
&lt;Image x:Name=&quot;AppFontIcon&quot;
HorizontalAlignment=&quot;Left&quot; 
VerticalAlignment=&quot;Center&quot;
Source=&quot;Assets/Square44x44Logo.png&quot; 
Width=&quot;16&quot; 
Height=&quot;16&quot;/&gt;
&lt;TextBlock x:Name=&quot;AppTitle&quot;
Text=&quot;Aplikacja Szpitalna&quot;
VerticalAlignment=&quot;Center&quot;
Margin=&quot;12, 0, 0, 0&quot;
Style=&quot;{StaticResource CaptionTextBlockStyle}&quot; /&gt;
&lt;/StackPanel&gt;
&lt;/Border&gt;
&lt;muxc:NavigationView x:Name=&quot;NavigationViewControl&quot;
IsTitleBarAutoPaddingEnabled=&quot;False&quot;            
IsBackButtonVisible=&quot;Visible&quot;           
DisplayModeChanged=&quot;NavigationViewControl_DisplayModeChanged&quot;   
Canvas.ZIndex=&quot;0&quot;&gt;
&lt;muxc:NavigationView.Header&gt;
&lt;Grid HorizontalAlignment=&quot;Left&quot; Padding=&quot;0&quot; Margin=&quot;6,0,0,0&quot;&gt;
&lt;TextBlock Text=&quot;Dashboard&quot; HorizontalAlignment=&quot;Center&quot; FontSize=&quot;20&quot; /&gt;
&lt;/Grid&gt;
&lt;/muxc:NavigationView.Header&gt;
&lt;muxc:NavigationView.AutoSuggestBox&gt;
&lt;AutoSuggestBox 
PlaceholderText=&quot;Wyszukaj&quot; 
QueryIcon=&quot;Find&quot;                     
x:Name=&quot;controlsSearchBox&quot;
TextChanged=&quot;ControlsSearchBox_TextChanged&quot;
VerticalAlignment=&quot;Center&quot;
x:FieldModifier=&quot;public&quot;
MinWidth=&quot;200&quot;
KeyboardAcceleratorPlacementMode=&quot;Hidden&quot;&gt;
&lt;/AutoSuggestBox&gt;
&lt;/muxc:NavigationView.AutoSuggestBox&gt;
&lt;muxc:NavigationView.MenuItems&gt;
&lt;muxc:NavigationViewItemSeparator /&gt;
&lt;muxc:NavigationViewItem Icon=&quot;Home&quot; Content=&quot;Menu Gł&#243;wne&quot;/&gt;
&lt;muxc:NavigationViewItemSeparator /&gt;
&lt;!--Sekcja techniczna--&gt;
&lt;muxc:NavigationViewItemHeader Content=&quot;Techniczne&quot; Margin=&quot;0,20,0,0&quot;/&gt;
&lt;muxc:NavigationViewItem Icon=&quot;List&quot; Content=&quot;System Kolejkowy&quot;/&gt;
&lt;muxc:NavigationViewItem Icon=&quot;FourBars&quot; Content=&quot;Networking&quot;&gt;
&lt;muxc:NavigationViewItem.MenuItems&gt;
&lt;muxc:NavigationViewItem Content=&quot;Serwery&quot; Icon=&quot;AllApps&quot;/&gt;
&lt;muxc:NavigationViewItem Content=&quot;Urządzenia aktywne&quot; Icon=&quot;Manage&quot;/&gt;
&lt;muxc:NavigationViewItem Content=&quot;Punkty PPD&quot; Icon=&quot;Map&quot;/&gt;
&lt;muxc:NavigationViewItem Content=&quot;Adresy IP&quot; Icon=&quot;GoToStart&quot;/&gt;
&lt;muxc:NavigationViewItem Content=&quot;Dostęp zdalny&quot; Icon=&quot;Globe&quot;/&gt;
&lt;muxc:NavigationViewItem Content=&quot;Skaner ARP&quot; Icon=&quot;Find&quot;/&gt;
&lt;/muxc:NavigationViewItem.MenuItems&gt;
&lt;/muxc:NavigationViewItem&gt;
&lt;/muxc:NavigationView.MenuItems&gt;
&lt;/muxc:NavigationView&gt;
&lt;/Grid&gt;
&lt;/Page&gt;

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&lt;NavigationViewItem&gt;()
.Where(item =&gt; 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,还有NavigationViewItemSeparatorNavigationViewItemHeader

请尝试以下代码:

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.NavigationViewItemNavigationViewControl.MenuItems是特定的类和属性,未提供上下文信息,翻译时保留了原文。

英文:

There are not only NavigationViewItem in the NavigationViewControl.MenuItems, but also NavigationViewItemSeparator and NavigationViewItemHeader in it.

Please try the following code:

    public List&lt;string&gt; CheckItems( List&lt;object&gt; sourceList, List&lt;string&gt; 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&lt;object&gt; level2Items = viewItem.MenuItems.ToList();
if (level2Items.Count &gt; 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&lt;string&gt; resultList = new List&lt;string&gt;();
List&lt;object&gt; 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 &amp;&amp;
            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;
    }
}

huangapple
  • 本文由 发表于 2023年1月9日 06:55:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定