【问题标题】:<Entry.Behaviors> doesn't work inside ListView ItemTemplate<Entry.Behaviors> 在 ListView ItemTemplate 中不起作用
【发布时间】:2021-02-07 01:38:06
【问题描述】:

我在 ListView 的 ItemTemplate 中使用 Entry 视图,并且我想将 Command 绑定到 Entry 视图的 Completed 事件(ala MVVM)。 带有 DataTemplate 定义的 XAML 顶部如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:models="clr-namespace:MedLemnMobile.Models"
             xmlns:viewModel="clr-namespace:MedLemnMobile.ViewModels"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="MedLemnMobile.Views.EquationLibPage"
             Title="EquationSets"
             Focused="ContentPage_Focused"
             x:FieldModifier="public"
             x:Name="myEqLibPage"
             AutomationId="myEqLibPage"
             BackgroundColor="{DynamicResource PageBackgroundColor}"
             x:DataType="viewModel:EquationLibViewModel">

    <ContentPage.BindingContext>
        <viewModel:EquationLibViewModel/>
    </ContentPage.BindingContext>

    <ContentPage.Resources>
        <DataTemplate x:Key="myEqSetDataTemplateViewCell_1">
            <ViewCell>
                <ViewCell.View Background="Goldenrod">
                    <StackLayout>
                        <Entry x:FieldModifier="public" x:Name="myEquationSetNameEntry"
                               x:DataType="models:EquationSet"
                               Text="{Binding EquationSetName, Mode=TwoWay}"
                               TextColor="{DynamicResource TextForegroundColor}">
                            <Entry.Behaviors>
                                <xct:EventToCommandBehavior
                                    x:DataType="viewModel:EquationLibViewModel"
                                    EventName="Completed"
                                    Command="{Binding EquationSetNameCompletedCommand}"
                                    CommandParameter="{x:Reference myEqLibPage}"/>
                            </Entry.Behaviors>
                        </Entry>
                    </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>

稍后在 XAML 中,ListView 定义如下所示:

        <Grid Grid.Row="1" Grid.Column="0">
            <ListView   x:FieldModifier="public" x:Name="myEquationSetsListView"
                        AutomationId="myEquationSetsListView"
                        ItemsSource="{Binding EquationLib}"
                        SelectionMode="Single"
                        ItemTemplate="{StaticResource myEqSetDataTemplateViewCell_1}">
            </ListView>
        </Grid>

而且,ViewModel 中的 Command 声明如下所示:

namespace MedLemnMobile.ViewModels
{
    public class EquationLibViewModel : ViewModelBase
    {
        public ICommand GotoMenuCommand { get; }
        public ICommand GotoParamsCommand { get; }
        public ICommand EquationSetNameCompletedCommand { get; }

        public EquationLibViewModel()
        {
            GotoMenuCommand = new Command<EquationLibPage>(GotoMenu);
            GotoParamsCommand = new Command<EquationLibPage>(GotoParams);
            EquationSetNameCompletedCommand = new Command<EquationLibPage>(EquationSetNameCompleted);
        }

我希望当用户“完成”在 Entry 视图中输入值时,Command EquationSetNameCompletedCommand 将触发。 我在命令的代码开头设置了一个断点,断点永远不会触发。 任何帮助弄清楚为什么这个命令没有触发将不胜感激。

【问题讨论】:

    标签: listview datatemplate eventtocommand


    【解决方案1】:

    我让它工作了!答案在命令绑定中如下:

                        <Entry.Behaviors>
                            <xct:EventToCommandBehavior
                                x:DataType="viewModel:EquationLibViewModel"
                                EventName="Completed"
                                Command="{Binding Path=BindingContext.EquationSetNameCompletedCommand,
                                          Source={Reference myEqLibPage}}"
                                CommandParameter="{Reference myEqLibPage}"/>
                        </Entry.Behaviors>
    

    【讨论】:

      猜你喜欢
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      相关资源
      最近更新 更多