【问题标题】:Xamarin.Forms (PRISM) - Bind checkbox, to a command in viewmodel, outside listview itemsourceXamarin.Forms (PRISM) - 将复选框绑定到 viewmodel 中的命令,户外 listview itemssource
【发布时间】:2020-12-17 14:47:05
【问题描述】:

Sooo,我正在构建一个 Prism,xamarin.forms 应用程序。

我有一个名为“Herd”的对象,具有基本属性。

我正在显示这些对象的列表,并希望激活一个命令,将复选框中选中的牛群发送到视图模型。

<ListView
                x:Name="UpdateHerdList"
                ItemsSource="{Binding HerdsThatsNeedsToBeUpdated}"
                RowHeight="60"
                >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <CustomRenderer:TransparentViewCell>
                            <Grid
                               >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="1*"/>
                                    <ColumnDefinition Width="50"/>
                                </Grid.ColumnDefinitions>
                                <Label 
                                    TextColor="Black"
                                    FontSize="15"
                                    VerticalTextAlignment="Center"
                                    HorizontalTextAlignment="Start"
                                    Grid.Column="0"
                                    Text="{Binding HeaderName}"
                                    />
                                    <CheckBox 
                                        Grid.Column="1"
                                        HorizontalOptions="Center" 
                                        ScaleX="1.75" 
                                        ScaleY="1.75"
                                        Color="Black"
                                        IsChecked="True"
                                        >
                                        <CheckBox.Behaviors>
                                            <b:EventToCommandBehavior
                                                EventName="PropertyChanged"
                                                Command="{Binding UpdateThisHerdCommand}"
                                                CommandParameter="{Binding Herd}"
                                                />
                                        </CheckBox.Behaviors>
                                    </CheckBox>
                                </Grid>
                        </CustomRenderer:TransparentViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

但我似乎找不到正确的方法来绑定到列表视图的 itemsource 内的视图模型。

有人可以帮助指导我正确的装订方式吗?

提前致谢!

【问题讨论】:

标签: c# xaml xamarin.forms mvvm


【解决方案1】:

关于将一个命令绑定到复选框,我做了一个示例,您可以看到以下代码。如果您更改复选框选中,将传递当前列表视图项。

 <ListView
            x:Name="UpdateHerdList"
            ItemsSource="{Binding HerdsThatsNeedsToBeUpdated}"
            RowHeight="60">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="1*" />
                                <ColumnDefinition Width="50" />
                              
                            </Grid.ColumnDefinitions>
                            <Label
                                Grid.Column="0"
                                FontSize="15"
                                HorizontalTextAlignment="Start"
                                Text="{Binding HeaderName}"
                                TextColor="Black"
                                VerticalTextAlignment="Center" />
                            <CheckBox
                                Grid.Column="1"
                                HorizontalOptions="Center"
                                IsChecked="True"
                                ScaleX="1.75"
                                ScaleY="1.75"
                                Color="Black">

                                <CheckBox.Behaviors>
                                    <b:EventToCommandBehavior
                                        Command="{Binding BindingContext.UpdateThisHerdCommand, Source={x:Reference UpdateHerdList}}"
                                        CommandParameter="{Binding .}"
                                        EventName="CheckedChanged" />
                                </CheckBox.Behaviors>
                            </CheckBox>
                          
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

public partial class Page34 : ContentPage
{
    public ObservableCollection<Herd> HerdsThatsNeedsToBeUpdated { get; set; }
    public ICommand UpdateThisHerdCommand { get; set; }
    public Page34()
    {
        InitializeComponent();

        HerdsThatsNeedsToBeUpdated = new ObservableCollection<Herd>()
        {
            new Herd(){HeaderName="test 1"},
            new Herd(){HeaderName="test 2"},
            new Herd(){HeaderName="test 3"},
            new Herd(){HeaderName="test 4"},
            new Herd(){HeaderName="test 5"},
            new Herd(){HeaderName="test 6"}

        };

        UpdateThisHerdCommand = new Command<Herd>(checkboxcommand);

        this.BindingContext = this;
    }

    private void checkboxcommand(Herd herd)
    {
        Console.WriteLine("the selected item is {0}",herd.HeaderName);
    }
}

public class Herd
{
    public string HeaderName { get; set; }
}

【讨论】:

    猜你喜欢
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多