我们如何将xamarin形式的通用视图模型中的列表项“网格宽度”绑定到“属性”?

Midhun Kumar

我有一个GridViewCard,它在Xamarin表单应用程序的多个屏幕中用作数据模板。所有这些页面都有一个从基本视图模型扩展的视图模型。是否可以将GridCard的宽度绑定到基本视图模型中的GridLength属性,而不依赖于使用它的页面/列表?

GridViewCard:

<Grid xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms" xmlns:i18n="clr-namespace:ScholarApp.Assets.Extensions;assembly=ScholarApp" xmlns:controls="clr-namespace:ScholarApp.Assets.Controls" xmlns:Pancake="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView" xmlns:converters="clr-namespace:ScholarApp.Assets.Converters" x:Class="ScholarApp.Assets.Templates.GridViewCard" RowSpacing="10" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Padding="{OnIdiom Phone='20,10,0,20',Tablet='30,15,0,30'}">
    <Grid.RowDefinitions>
        <RowDefinition Height="{OnIdiom Phone='200',Tablet='280'}" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{"**this value should be dynamically bound to the page in which it is being used**" }" />
    </Grid.ColumnDefinitions>

</Grid>

多个页面中的多个用例

一页中的代码段:

<StackLayout Style="{DynamicResource Default}" Padding="0,5,0,0" IsVisible="{Binding IsViewVisible}">
                <flv:FlowListView x:Name="ExploreGridView" IsVisible="{Binding IsGridLayout}" BackgroundColor="Transparent" Margin="{OnIdiom Phone='0,0,20,0',Tablet='0,0,30,0'}" FlowColumnCount="{OnIdiom Phone='2',Tablet='3'}" SeparatorVisibility="None" HasUnevenRows="true" FlowItemsSource="{Binding HandpickedList}" VerticalScrollBarVisibility="Never">
                    <flv:FlowListView.FlowColumnTemplate>
                        <DataTemplate>
                            <templates:GridViewCard />
                        </DataTemplate>
                    </flv:FlowListView.FlowColumnTemplate>
                </flv:FlowListView>
            </StackLayout>

另一页上的代码段:

<ScrollView VerticalScrollBarVisibility="Never"
                HorizontalScrollBarVisibility="Never"
                Orientation="Horizontal">
        <StackLayout Padding="{DynamicResource ExploreStkPadding20}"
                     Orientation="Horizontal"
                     Spacing="{OnIdiom Phone='20',Tablet='30'}"
                     HeightRequest="{DynamicResource HandpickedHeight}"
                     BindableLayout.ItemsSource="{Binding ViewData}">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <templates:GridViewCard />
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>
    </ScrollView>
松树

要做到这一点,您需要做一些事情:

首先,添加一个名字(x:Name)将GridGridViewCardXAML文件。您稍后将使用它。

<Grid xmlns="http://xamarin.com/schemas/2014/forms"
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
      x:Class="SQDictionary.GridViewCard"
      RowSpacing="10"
      x:Name="LeGrid"
      HorizontalOptions="FillAndExpand"
      BackgroundColor="BlueViolet"
      VerticalOptions="FillAndExpand"
      Padding="{OnIdiom Phone='20,10,0,20',Tablet='30,15,0,30'}">

2:BindablePropertyGridViewCard类文件GridViewCard.cs中添加一个

public static readonly BindableProperty GridWidthProperty =
        BindableProperty.Create(nameof(GridWidth),
                                typeof(GridLength),
                                typeof(GridViewCard),
                                default(GridLength),
                                propertyChanged:OnGridLengthChanged);

public GridLength GridWidth
{
    get => (GridLength)GetValue(GridWidthProperty);
    set { SetValue(GridWidthProperty, value); }
}

private static void OnGridLengthChanged(BindableObject bindable, object oldValue, object newValue)
{
    if (bindable is GridViewCard gridView && newValue is GridLength value)
    {
        var column0 = gridView.LeGrid.ColumnDefinitions?.ElementAtOrDefault(0);
        if (column0 != null)
        {
            column0.Width = value;
        }
    }
}

此代码将GridLength在UserControl中公开该属性,并在属性更改时更新“网格列”定义(宽度)。

此代码仅设置第一列的宽度(如您所指示的只有一个)。但是,如果要添加更多列并使用相同的值,则只需要遍历ColumnDefinition集合即可。

3:无论何时使用GridViewCard控件,您都可以访问我们刚刚创建的属性GridWidth并设置一个值。

<DataTemplate>
    <templates:GridViewCard GridWidth="130" />
</DataTemplate>

但是您已经表明要绑定ViewModel中的值。

由于您在Template内部使用此自定义控件,并且该值来自ViewModel(作为属性,而不是绑定到“ List”的集合的一部分,因此您需要做一些技巧。

让我们将该属性添加到中BaseViewModel

private double _gridColumnLength;
public double GridColumnLength
{
    get => _gridColumnLength;
    set
    {
       //Don't know how you are handling the **Set** and/or Raising of property changes.
      // Modify accordingly (if required)
        _gridColumnLength = value;
        RaisePropertyChanged(nameof(GridColumnLength));
    }
}

现在,以FlowList为例:

您将使用x:NameFlowList中的名称()访问BindingContext,然后访问Property的值。

<flv:FlowListView x:Name="ExploreGridView" 
                IsVisible="{Binding IsGridLayout}" 
                BackgroundColor="Transparent" 
                Margin="{OnIdiom Phone='0,0,20,0',Tablet='0,0,30,0'}" 
                FlowColumnCount="{OnIdiom Phone='2',Tablet='3'}" 
                SeparatorVisibility="None"                 
                HasUnevenRows="true" 
                FlowItemsSource="{Binding HandpickedList}" 
                VerticalScrollBarVisibility="Never">
    <flv:FlowListView.FlowColumnTemplate>
        <DataTemplate>
            <templates:GridViewCard GridWidth="{Binding BindingContext.GridColumnLength, Source={x:Reference ExploreGridView}}" />
        </DataTemplate>
    </flv:FlowListView.FlowColumnTemplate>
</flv:FlowListView>

现在,在GridColumnLength有权访问的ViewModels属性上设置值时,将触发对自定义控件上Grid.Column.Width的更改GridViewCard

希望这可以帮助。-

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我们如何将数据绑定到 Xamarin Forms 中的“页脚”

来自分类Dev

如何将视图的BackgroundColor属性绑定到Xamarin Forms中的视图模型?

来自分类Dev

Silverlight 如何将属性绑定到列表项值

来自分类Dev

如何将包含列表的字典绑定到网格视图

来自分类Dev

如何将列表模型绑定到视图?

来自分类Dev

如何将DateTime属性绑定到我的视图模型?

来自分类Dev

如何将DateTime属性绑定到我的视图模型?

来自分类Dev

如何将SwitchCell文本颜色绑定到Xamarin.Forms中的视图模型

来自分类Dev

如何将多个模型中的数据绑定到单个视图中的列表

来自分类Dev

我们如何将列表中的所有项目从模型类存储库传递到控制器,然后进行查看?

来自分类Dev

如何将下拉列表中的 selectlistitem 值绑定到模型中的字符串属性

来自分类Dev

MvvmCross-如何将UIView.Layer.AnyProperty(Xamarin.iOS)绑定到视图模型上的属性?

来自分类Dev

MvvmCross-如何将UIView.Layer.AnyProperty(Xamarin.iOS)绑定到视图模型上的属性?

来自分类Dev

如何将标签绑定到Xamarin中的属性?

来自分类Dev

如何将列表传递给模型,然后再绑定到网格

来自分类Dev

如何将 Dapper 检索到的列表分配给我的类模型中的属性?

来自分类Dev

如何将Monotouch UI元素的'enabled'属性绑定到Mvvmcross中的视图模型布尔值

来自分类Dev

我们如何将列表对象从jsp传递到struts2动作?

来自分类Dev

如何将DBnull值绑定到网格视图?

来自分类Dev

如何将视图模型中的功能绑定到swiftui中的自定义视图?

来自分类Dev

Universal Apps:如何将ListViewItem(容器)的属性绑定到实际项目(视图模型)?

来自分类Dev

我们如何将UIButton存储到NSDictionary?

来自分类Dev

如何将剃刀视图绑定到带有对象列表的模型?

来自分类Dev

如何将网格绑定到WPF中的选定树视图项目

来自分类Dev

我们如何将验证码集成到iOS应用程序中?

来自分类Dev

我们如何将参数传递到nodeJS中的已安装路由?

来自分类Dev

我们如何将Captcha集成到iOS应用程序中?

来自分类Dev

我们如何将 json 列表转换为 postgres 中的行?

来自分类Dev

将视图模型绑定到敲除中属性的存在

Related 相关文章

  1. 1

    我们如何将数据绑定到 Xamarin Forms 中的“页脚”

  2. 2

    如何将视图的BackgroundColor属性绑定到Xamarin Forms中的视图模型?

  3. 3

    Silverlight 如何将属性绑定到列表项值

  4. 4

    如何将包含列表的字典绑定到网格视图

  5. 5

    如何将列表模型绑定到视图?

  6. 6

    如何将DateTime属性绑定到我的视图模型?

  7. 7

    如何将DateTime属性绑定到我的视图模型?

  8. 8

    如何将SwitchCell文本颜色绑定到Xamarin.Forms中的视图模型

  9. 9

    如何将多个模型中的数据绑定到单个视图中的列表

  10. 10

    我们如何将列表中的所有项目从模型类存储库传递到控制器,然后进行查看?

  11. 11

    如何将下拉列表中的 selectlistitem 值绑定到模型中的字符串属性

  12. 12

    MvvmCross-如何将UIView.Layer.AnyProperty(Xamarin.iOS)绑定到视图模型上的属性?

  13. 13

    MvvmCross-如何将UIView.Layer.AnyProperty(Xamarin.iOS)绑定到视图模型上的属性?

  14. 14

    如何将标签绑定到Xamarin中的属性?

  15. 15

    如何将列表传递给模型,然后再绑定到网格

  16. 16

    如何将 Dapper 检索到的列表分配给我的类模型中的属性?

  17. 17

    如何将Monotouch UI元素的'enabled'属性绑定到Mvvmcross中的视图模型布尔值

  18. 18

    我们如何将列表对象从jsp传递到struts2动作?

  19. 19

    如何将DBnull值绑定到网格视图?

  20. 20

    如何将视图模型中的功能绑定到swiftui中的自定义视图?

  21. 21

    Universal Apps:如何将ListViewItem(容器)的属性绑定到实际项目(视图模型)?

  22. 22

    我们如何将UIButton存储到NSDictionary?

  23. 23

    如何将剃刀视图绑定到带有对象列表的模型?

  24. 24

    如何将网格绑定到WPF中的选定树视图项目

  25. 25

    我们如何将验证码集成到iOS应用程序中?

  26. 26

    我们如何将参数传递到nodeJS中的已安装路由?

  27. 27

    我们如何将Captcha集成到iOS应用程序中?

  28. 28

    我们如何将 json 列表转换为 postgres 中的行?

  29. 29

    将视图模型绑定到敲除中属性的存在

热门标签

归档