Win UI3 绑定到 MVVM 中的 AutoSuggestBox

问题描述 投票:0回答:1
c# mvvm binding winui-3 xbind
1个回答
0
投票

首先,请安装以下nuget包

https://www.nuget.org/packages/Microsoft.Xaml.Behaviors.WinUI.Managed

然后您可以使用交互并在 xaml 中绑定您的方法(来自视图模型)

<AutoSuggestBox x:Name="Control2" PlaceholderText="Type a control name"
        TextChanged="SearchBox_TextChanged" QueryIcon="Find" Width="300" HorizontalAlignment="Left">
        <i:Interaction.Behaviors>
            <ic:EventTriggerBehavior EventName="QuerySubmitted">
                <ic:EventTriggerBehavior.Actions>
                    <ic:InvokeCommandAction Command="{x:Bind ViewModel.SearchBox_QuerySubmitted}" />
                </ic:EventTriggerBehavior.Actions>
            </ic:EventTriggerBehavior>
            <ic:EventTriggerBehavior EventName="SuggestionChosen">
                <ic:EventTriggerBehavior.Actions>
                    <ic:InvokeCommandAction Command="{x:Bind ViewModel.SearchBox_SuggestionChosen}" />
                </ic:EventTriggerBehavior.Actions>
            </ic:EventTriggerBehavior>
        </i:Interaction.Behaviors>
</AutoSuggestBox>

和csharp:

public void SearchBox_QuerySubmitted(AutoSuggestBoxQuerySubmittedEventArgs args)
{

}

public void SearchBox_SuggestionChosen(AutoSuggestBoxSuggestionChosenEventArgs args)
{

}
© www.soinside.com 2019 - 2024. All rights reserved.