我有一个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
)将Grid
在GridViewCard
XAML文件。您稍后将使用它。
<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:BindableProperty
在GridViewCard
类文件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:Name
FlowList中的名称()访问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] 删除。
我来说两句