c# - Xamarin Forms 更改 CollectionView 中元素的大小
问题描述
我有这个代码:
<ContentPage.Content>
<StackLayout HorizontalOptions="Fill" Padding="15">
<Frame IsClippedToBounds="True" HeightRequest="45" CornerRadius="5" Padding="0" BackgroundColor="Transparent">
<Entry Placeholder="Search" ReturnType="Done" PlaceholderColor="Gray" x:Name="txtSearch" Margin="5,0,0,0" TextColor="White" />
</Frame>
<CollectionView ItemsSource="{Binding sourceList}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"
Span="2" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<ff:CachedImage
Source="{Binding Source}"
VerticalOptions="Center"
HorizontalOptions="Fill"
x:Name="TemplateImage" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</ContentPage.Content>
现在我希望我能做到:
public TemplateList()
{
InitializeComponent();
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
var width = mainDisplayInfo.Width;
var density = mainDisplayInfo.Density;
var ScaledWidth = width / density;
var WidthHeight = (ScaledWidth - (3 * 10)) / 2;
SizeChanged += (s, a) =>
{
TemplateImage.HeightRequest = WidthHeight;
TemplateImage.WidthRequest = WidthHeight;
};
}
但我得到一个错误:
TemplateImage
当前上下文中不存在该名称
现在我已经在我的 xaml 中用这一行声明了它:
<ff:CachedImage
Source="{Binding Source}"
VerticalOptions="Center"
HorizontalOptions="Fill"
x:Name="TemplateImage" />
现在如何在 C# 中更改 CachedImage 的大小?
解决方案
正确的方法是在XAMLHeightRequest
中绑定您的属性,如下所示CachedImage
<ff:CachedImage
Source="{Binding Source}"
HeightRequest="{Binding Source=MyViewModel Path=wishedHeight}"
VerticalOptions="Center"
HorizontalOptions="Fill" />
wishedHeight
视图模型中的属性在哪里。
注意不能直接使用
HeightRequest="{Binding wishedHeight}"
因为 wishedHeight 不是TemplateSource
您的 CachedImage 绑定到的类的一部分!
推荐阅读
- xslt-2.0 - 如何匹配元素中区分大小写的字符
- php - 如何在 PHP 中用已有的数字替换字符串
- cordova - Ioniv-v4:离子cordova插件添加cordova-plugin-googleplus不工作
- xcode - macOS NSExtensionMainStoryboard 无法加载
- ionic-framework - 如何在 ngSwitchCase 中加载离子图像?
- sql - 如何提高以下查询的性能
- python-3.x - couchbase 合并数组到子文档
- node.js - 这是 Node.js 项目的常见文件夹结构吗?
- r - 将向量剪切两部分并在索引为 1 时粘贴
- python - 如何在 Python 中处理从数据库中获取的单个和多个电子邮件 ID